1
votes

I have a number of Dynamic XFA PDFs that were created with Livecycle Designer. These PDFs are used as templates for various individuals to complete. When a user requests a template we have to write a submit button with url pointing back to a .net application for processing and update some of the fields with information from the database into the PDF.

Can I use iText(Sharp) with .net to update the dynamic xfa pdf to write into it a submit button and update fields and then use iText(Sharp) to process the returning form.

We are doing this now with Acroforms but need to do the same for Dynamic XFA forms as well. I can’t find any confirmation information that this is possible. If it is possible does anyone have any code that they can share to show me how to do this?

1
XFA is very different from AcroForm technology. Changing an XFA form requires that you [a.] extract the XML that describes the form from its PDF container (possible with iText), [b.] update the XML (outside the scope of iText: do this with any XML manipulation tool), then [c.] put back the XML into the PDF containter (possible with iText). [a.] and [c.] are easy to answer; [b.] requires that you fully understand the XFA specification.Bruno Lowagie

1 Answers

1
votes

You can also achieve that putting the pdf's content inside an XmlDocument and working as you were working with an XML. This is the code I use to replace some placeholders written in textboxes.

PdfReader pdfReader = new PdfReader(path_pdf);
using (PdfStamper pdfStamp = new PdfStamper(pdfReader, new FileStream(temp_path, FileMode.Create), '\0', true))
{
    pdfStamp.ViewerPreferences = PdfWriter.AllowModifyContents;
    XmlDocument xmlDocument = pdfReader.AcroFields.Xfa.DomDocument;
    string pdfContent = xmlDocument.InnerXml;
    string newpdfContent = pdfContent
        .Replace("$CONTENT_TO_REPLACE_1$", "some_content")
        .Replace("$CONTENT_TO_REPLACE_2$", "some_other_content")

    xmlDocument.InnerXml = newpdfContent;
    Stream stream = GenerateStreamFromString(newpdfContent);
    pdfStamp.AcroFields.Xfa.FillXfaForm(stream);
    pdfStamp.AcroFields.Xfa.DomDocument = xmlDocument;
    pdfStamp.Close();
}