填充动态XFA PDF格式的iText [英] Populate dynamic XFA pdf form itext
问题描述
我有,我们需要使用Java中填充XFA基于PDF表单。 你可以建议最好的办法。 我能够生成XFA XML利用iText的PDF。
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();
}
我的PDF阅读器和code以上生成XFA XML。 能否请你建议我如何进一步继续,我似乎没了主意。我试图检查XFA文档,但看起来不正确。 我没有一个XML和PDF格式是非常复杂的,因为它有许多领域,是一个动态的XFA PDF表单。
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.
您的帮助和建议将竭诚AP preciated。
Your help and suggestions will be sincerely appreciated.
推荐答案
请看看在 FillXFA
的例子。这个例子有一个表格( purchase_order.pdf ),其中包含一个动态表:
Please take a look at the FillXFA
example. This example has a form (purchase_order.pdf) that contains a dynamic table:
现在,我们将 data.xml的,这是一组数据一个XML文件的形式,我们得到这样的结果: purchase_order_filled.pdf
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
正如你所看到的,数据present在XML文件中加入,还有一些空行(与在XML空数据行对应)。当我们增加了许多行,我们甚至引发了新的一页添加到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.
这是它是如何做的:
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();
}
还有另外一个例子在这里: XfaMovies
。该 xfa_movies.pdf 文件是没有任何按钮的单页PDF文件。我们注入的120电影中的数据: movies.xml 。其结果是23页PDF, xfa_filled_in.pdf 。
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.
注意:阅读你的code,你会得到一个包含完整的XFA流的XML。你不需要,你只需要的数据。这就是为什么我添加链接到XML文件:它们不包含任何XFA特定的标签
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.
这篇关于填充动态XFA PDF格式的iText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!