1

Within Adobe Acrobat XI, when editing PDF forms, there are functions under

Tools -> Forms -> More Form Options -> Import Data
Tools -> Forms -> More Form Options -> Export Data

Import Data takes an XML file and imports the data into the PDF. Export obviously creates an XML file from the data entered into the current form.

I need to mimic this functionality in a .Net application. (ideally web based).

Are there any 3rd party libraries (iTextSharp?) that can take a PDF file and an XML file and output a PDF that has imported the data from the XML? Or would using Acrobat native libraries be best for automating this?

Does anyone have an example of doing something similar to this using either a 3rd party library or Adobe components?

Note: The PDF form that I would need to import/export from is NOT created internally. Specifically I need to do this with a PDF form created by the patent office. (SB08a Information Disclosure Statement)

http://www.uspto.gov/patents/process/file/efs/guidance/updated_IDS.pdf

Thanks!

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
eoldre
  • 1,075
  • 18
  • 28
  • 1
    Check this out http://stackoverflow.com/a/5638922/231316 as well as http://stackoverflow.com/questions/tagged/xfa+itextsharp – Chris Haas Dec 07 '13 at 19:34

1 Answers1

1

I discovered I could get the behavior I needed from the ITextSharp library.

    /// <summary>
    /// Exports XFA data from a PDF File.
    /// </summary>
    /// <param name="populatedPDFForm">a readable stream of the PDF with a populated form</param>
    /// <returns>A stream containing the exported XML form data</returns>
    public static System.IO.MemoryStream Export(System.IO.Stream populatedPDFForm)
    {
        System.IO.MemoryStream outputStream = new System.IO.MemoryStream();
        using (iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(populatedPDFForm))
        {
            var settings = new System.Xml.XmlWriterSettings { Indent = true };
            using (var writer = System.Xml.XmlWriter.Create(outputStream, settings))
            {
                reader.AcroFields.Xfa.DatasetsNode.WriteTo(writer);
            }
        }
        return outputStream;
    }

    /// <summary>
    /// Imports XFA Data into a new PDF file.
    /// </summary>
    /// <param name="pdfTemplate">A PDF File with an unpopulated form.</param>
    /// <param name="xmlFormData">XFA form data in XML format.</param>
    /// <returns>a memorystream containing the new PDF file.</returns>
    public static System.IO.MemoryStream Import(System.IO.Stream pdfTemplate, System.IO.Stream xmlFormData)
    {
        System.IO.MemoryStream outputSteam = new System.IO.MemoryStream();
        using (iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(pdfTemplate))
        {
            using (iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outputSteam))
            {
                stamper.Writer.CloseStream = false;
                stamper.AcroFields.Xfa.FillXfaForm(xmlFormData);
            }
        }
        return outputSteam;
    }
eoldre
  • 1,075
  • 18
  • 28
  • I tried both functions, Export() works fine, but in Import() there was one issue. I had quite complex pdf form and after importing data, I tried to open it, and Adobe Reader anounced file is corrupted. Solution for this was to allow stamper to append to existing pdf by extending parameters on this line: `using (iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outputSteam, '\0', true))` – Petro Kostyuk Sep 11 '14 at 09:01