问题
I have a XFA based pdf form that we need to be populate using java. Can you suggest the best approach. I was able to generate the xfa xml for the pdf using iText.
public void readXfa(String srcPdfFilename, String destXMLFilename) throws IOException, ParserConfigurationException, SAXException, TransformerFactoryConfigurationError, TransformerException {
PdfReader reader = new PdfReader(srcPdfFilename);
XfaForm xfa = new XfaForm(reader);
Document doc = xfa.getDomDocument();
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
FileOutputStream os = new FileOutputStream(destXMLFilename);
tf.transform(new DOMSource(doc), new StreamResult(os));
reader.close();
}
I have the Pdf and Xfa XML generated from code above. Can you please suggest me how to proceed further as I seem to be out of ideas. I tried to check the XFA documentation but does not seem right. I do not have an xml and the pdf is very complex as it has many fields and is a dynamic XFA pdf form.
Your help and suggestions will be sincerely appreciated.
回答1:
Please take a look at the FillXFA example. This example has a form (purchase_order.pdf) that contains a dynamic table:
Now we add data.xml, which is a set of data in the form of an XML file, we get this result: purchase_order_filled.pdf
As you can see, the data present in the XML file was added, as well as a number of empty rows (which correspond with empty data rows in the XML). As we added many rows, we even triggered a new page to be added to the PDF.
This is how it's done:
public void manipulatePdf(String src, String dest)
throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
XfaForm xfa = form.getXfa();
xfa.fillXfaForm(new FileInputStream(XML));
stamper.close();
reader.close();
}
There's another example here: XfaMovies. The xfa_movies.pdf document is a single page PDF without any buttons. We inject the data of 120 movies: movies.xml. The result is the 23-page PDF, xfa_filled_in.pdf.
NOTE: reading your code, you get an XML that contains the complete XFA stream. You don't need that, you only need the data. That's why I added the links to the XML files: they don't contain any XFA specific tags.
来源:https://stackoverflow.com/questions/24508342/populate-dynamic-xfa-pdf-form-itext