Question

I need to implement a method to merge fields in a PDF with forms created in Adobe Life Cycle. I'll receive the template PDF and a XML to populate the PDF and need to return the new filled file. The xml is something like this:

 <?xml version="1.0" encoding="UTF-8"?>
<form1>
    <ReportDescription>
      <body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
              <p><span style="font:Arial bold 12px">Name of the document</span></p>
          </body>
    </ReportDescription>
    <ReportCode>XX-000</ReportCode>
   <Contents>
      <UserData>
              <UserName>Ego ille</UserName>
              <UserPhone>Si manu vacuas</UserPhone>
              <UserNIF>999999999</UserNIF>
      </UserData>
   </Contents>
</form1>

So, I have something like the following:

private MemoryStream GeneratePDF(string m_FormName, XmlDocument oData)
        {
            PdfReader pdfTemplate;
            PdfStamper stamper;
            PdfReader tempPDF;
            Document doc;
            MemoryStream msTemp;
            PdfWriter pCopy;
            MemoryStream msOutput = new MemoryStream();

            pdfTemplate = new PdfReader(m_FormName);

            doc = new Document();
            pCopy = new PdfCopy(doc, msOutput);

            pCopy.AddViewerPreference(PdfName.PICKTRAYBYPDFSIZE, new PdfBoolean(true));
            pCopy.AddViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);

            doc.Open();

            for (int i = 1; i < pdfTemplate.NumberOfPages + 1; i++)
            {
                msTemp = new MemoryStream();
                pdfTemplate = new PdfReader(m_FormName);

                stamper = new PdfStamper(pdfTemplate, msTemp);

                // map xml values to pdf form controls (element name = control name)
                foreach (XmlElement oElem in oData.SelectNodes("/form1/*"))
                {
                    stamper.AcroFields.SetField(oElem.Name, oElem.InnerText);
                }

                stamper.FormFlattening = true;
                stamper.Close();
                tempPDF = new PdfReader(msTemp.ToArray());
                ((PdfCopy)pCopy).AddPage(pCopy.GetImportedPage(tempPDF, i));
                pCopy.FreeReader(tempPDF);

            }
            doc.Close();

            return msOutput;
        }

No correct solution

OTHER TIPS

Your question is somewhat misleading: you talk about merging forms that are created using Adobe LiveCycle. However, when I look at your code, I see that you are actually looking to merge ordinary PDFs. Allow me to explain.

Forms created using Adobe LiveCycle can result in two types of PDF files.

  1. Hybrid PDF files that contain the form in the form of PDF syntax (AcroForm technology) as well as XML (XML Forms Architecture, aka XFA).
  2. PDF files that are nothing but a container for XML.

Hybrid PDF files can be filled out using the core iText library. This is explained in Chapter 8 of my book. If you flatten such a form, you throw away the XML and you keep the PDF syntax. From that moment on, you have ordinary PDF files.

Pure XFA forms can be filled out with XFA Worker. XFA Worker is a closed source product built on top of iText. It parses the XML inside the PDF container and converts such a PDF to an ordinary PDF.

From your question, it's not clear which type of Adobe LiveCycle Form you're talking about, but since you're posting a question about it, it's safe to assume that you're experiencing a problem. Looking at your code, you're making the assumption that you're dealing with a hybrid form, and if that code doesn't work, we in turn could make the assumption that the form is a pure XFA form.

Once you succeed in filling out and flattening the form, you can indeed use PdfCopy, although depending on the nature of your forms, you may prefer to use PdfSmartCopy (assuming that you're merging different instances of the same template).

This answer is based on a whole lot of assumptions. This explains the down votes and the comment.

For instance: suppose that you're really asking to merge two XFA forms (in the sense of two PDF containers of XML syntax), then your question is unanswerable. Only flattened forms can be merged.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top