19

I need to populate XFA form fields in a PDF (created with Adobe LiveCycle Designer). We're attempting to use iText (actually iTextSharp with C#) to parse the PDF, populate the XFA fields and then save the modified PDF back out.

All the examples I can find with iText (very few iTextSharp examples) talk about modifying AcroForm fields. This PDF does NOT have AcroForm fields and uses XFA only.

Pointers to any non-standard resources would be helpful (I've already done the requisite Googling on the topic and haven't found anything useful).

Code examples here would be awesome from anyone who has actually done what I'm trying to do.

Jay Stevens
  • 5,863
  • 9
  • 44
  • 67
  • 1
    I just looked it up in the book "iText in action" and it reads "Forms like this aren't discussed in this book" so don't buy this book in hope of an answer. – David Waters Jul 01 '09 at 15:38

5 Answers5

5
using (FileStream existingPdf = new FileStream("existing.pdf", FileMode.Open))
using (FileStream sourceXml = new FileStream("source.xml", FileMode.Open))
using (FileStream newPdf = new FileStream("new.pdf", FileMode.Create))
{
    // Open existing PDF  
    PdfReader pdfReader = new PdfReader(existingPdf);

    // PdfStamper, which will create  
    PdfStamper stamper = new PdfStamper(pdfReader, newPdf);
    stamper.AcroFields.Xfa.FillXfaForm(sourceXml);

    stamper.Close();
    pdfReader.Close();
}
simaglei
  • 1,118
  • 1
  • 14
  • 14
  • 3
    This is great code, thanks! Could you please give us an example of the XML format to input to FillXfaForm? Thanks! –  May 27 '13 at 16:21
5

If you can get a data packet into the PDF, the XFA runtime in Acrobat would populate those fields with the data in the data packet.

If you want to see what one of these looks like, create a form in LiveCycle Designer (comes with Acrobat Pro), add some fields to it, and save it as a dynamic PDF. Open the form in Acrobat and type some values into the fields and save it.

Open the PDF with a tool that lets you peer at the PDF data and you'll find /Catalog/AcroForm/XFA a stream that has an <xfa:datasets> packet with the values you typed. That's what you'll need to create yourself and insert into the PDF.

The XDP spec includes a description of the data packet and the merge algorithm. You can find it here:

http://partners.adobe.com/public/developer/xml/index_arch.html

Alternately, you buy the LiveCycle server from Adobe which lets you do all this programmatically in a number of ways including through web service calls.

stevex
  • 5,589
  • 37
  • 52
  • This is useful information @stevex. MUCH appreciated. I did see the xfa:datasets chunk of data (or what I thought corresponds to that) when looking at the objects inside of iTextSharp. – Jay Stevens Aug 06 '09 at 15:02
5

iTextSharp can work with XFA. To remove all doubts, please take a look at sample on iText website:

http://itextpdf.com/examples/iia.php?id=165

Brad C
  • 2,868
  • 22
  • 33
Dmitry
  • 589
  • 4
  • 5
1

I have the same issue and I think I found the solution. I am using Powershell to inspect the pdf object.

Load the iTextSharp DLL.

Add-Type -Path "C:\Users\micah\Desktop\itextsharp.dll"

Load the PDF into a variable:

$PDF = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList "C:\Users\micah\Desktop\test.pdf"

Inspect this object:

$PDF.AcroFields.XFA.DomDocument.XDP.DataSets.Data.TopMostSubForm | Get-Member

What you SHOULD see, is that all your fields on your PDF are in this object as a property. You can get a quick view of all your fields like this:

$PDF.AcroFields.XFA.DomDocument.XDP.DataSets.Data.TopMostSubForm | Select-Object -Property "*"

That should be the magic ticket. This location is both fields from the original form AND the XFA portion of the form.

0

It says that in the book because itext does not do this. Can you convert your PDF?

  • No. It's a gov-generated form that I need to modify and then people will have to resubmit to gov. – Jay Stevens Jul 26 '09 at 16:50
  • 1
    Book is out of date - there has been some XFA support since 2006. For examples, see http://1t3xt.info/examples/browse/?page=example&id=433 and http://1t3xt.info/examples/browse/?page=example&id=441 – JasonPlutext Oct 25 '10 at 23:49