Extract xdp or xfa from PDF

6.5k views Asked by At

I created a PDF form with Adobe LiveCycle Designer. I'm now struggling to extract the data programmatically from the PDF after it's been filled out.

I tried to do this using poppler (the qt4 binding, but I guess that doesn't matter), but apparently poppler can't handle XFA forms. Although evince and okular are able to display the form...

As far as I understand, the PDF contains an XDP which in turn contains the XFA form. My question is, how can I extract that data from the PDF?

If there are libraries, c++, java, python or PHP are my options.

1

There are 1 answers

2
rhens On

The XML document (in XDP format) that makes up the XFA is stored as the value of the XFA key in the AcroForm dictionary (Interactive Form Dictionary). The AcroForm dictionary is referenced from the Catalog dictionary (Root of the PDF document).

The XFA value can be a stream or an array of streams. If it's a stream, it contains the entire XML document. If it's an array, the different streams contain the separate XDP packets. Concatenating them will give the full XML document.

One of the XDP packets is the dataSets packet. The actual form data will be in a child element of this packet: xfa:data. Example:

<xfa:dataSets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  <xfa:data>
    <!-- arbitrary XML data, e.g.: -->
    <Employee>
      <FirstName>John</FirstName>
      <Name>Doe</Name>
    </Employee>
  </xfa:data>
</xfa:dataSets>

Any PDF library that offers low-level access to PDF objects can be used to extract the XML document. Simply navigate through Catalog > AcroForm > XFA.

Some PDF libraries may offer a more high-level convenience method.

(Disclaimer: I'm an iText Software employee.) For example, using iText (Java) you can simply do this to get the XFA as an org.w3c.dom.Document:

PdfReader reader = new PdfReader(pdfFile);
XfaForm xfa = reader.getAcroFields().getXfa();
org.w3c.dom.Document doc = xfa.getDomDocument();

Or to just get the dataSets packet as an org.w3c.dom.Node:

org.w3c.dom.Node datasets = xfa.getDatasetsNode();