/** * Example written by Bruno Lowagie in answer to: * http://stackoverflow.com/questions/28601068/get-names-field-from-interactive-form-pdf */ package sandbox.xfa; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.AcroFields; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.XfaForm; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class ReadXFA { public static final String SRC = "resources/pdfs/xfa_form_poland.pdf"; public static final String DEST = "results/xfa/data.xml"; public static void main(String[] args) throws IOException, DocumentException, TransformerException { File file = new File(DEST); file.getParentFile().mkdirs(); new ReadXFA().readXml(SRC, DEST); } public void readXml(String src, String dest) throws IOException, DocumentException, TransformerException { PdfReader reader = new PdfReader(src); AcroFields form = reader.getAcroFields(); XfaForm xfa = form.getXfa(); Node node = xfa.getDatasetsNode(); NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { if("data".equals(list.item(i).getLocalName())) { node = list.item(i); break; } } list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { if("movies".equals(list.item(i).getLocalName())) { node = list.item(i); break; } } Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); FileOutputStream os = new FileOutputStream(dest); tf.transform(new DOMSource(node), new StreamResult(os)); reader.close(); } }