结合XFA和PDFBox [英] Combining XFA with PDFBox

查看:360
本文介绍了结合XFA和PDFBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用PDFBox java库填充PDF表单。
PDF表格是使用Adobe Live Designer创建的,所以它使用XFA格式。

I would like to fill a PDF form with the PDFBox java library. The PDF form is created with Adobe Live Designer, so it uses the XFA format.

我尝试使用PDFBox查找有关填充XFA PDF表单的资源,但到目前为止,我还没有运气。我看到API中有一个PDAcroForm.setXFA方法,但我不知道如何使用它。

I try to find resources about filling XFA PDF forms with PDFBox, but i haven't any luck so far. I saw that a PDAcroForm.setXFA method is available in the API, but i don't see how to use it.

你知道是否可以填充PDFBox与PDFBox?
如果是的话,是否有代码示例或教程来实现这一目标?
如果不是,那么最好的选择是什么?

Do you know if it is possible to fill a PDF Form with PDFBox ? If yes, is there anywhere a code sample or a tutorial to achieve this ? If no, what are the best alternatives to achieve this ?

推荐答案

学科;您不需要iText,可以使用PDFBox 1.8中提供的PDXFA对象完成XFA操作。

The question specifically identifies the PDFBox library in the subject; you do not need iText, the XFA manipulation can be done using the PDXFA object available in PDFBox 1.8.

非常感谢Maruan Sahyoun在PDFBox + XFA方面所做的出色工作。

Many thanks to Maruan Sahyoun for his great work on PDFBox + XFA.

此代码仅适用于删除PDDocument上的所有安全性。

它还假定PDXFA中的COS对象是COSStream。
下面的简单示例读取xml流并将其写回到PDF中。

This code only works when you remove all security on the PDDocument.
It also assumes the COS object in PDXFA is a COSStream. The simplistic example below reads the xml stream and writes it back into the PDF.

 PDDocument doc = PDDocument.load("filename");
 doc.setAllSecurityToBeRemoved(true);

 PDDocumentCatalog docCatalog = doc.getDocumentCatalog();
 PDAcroForm form = docCatalog.getAcroForm();

 PDXFA xfa = form.getXFA();
 COSBase cos = xfa.getCOSObject();
 COSStream coss = (COSStream) cos;
 InputStream cosin = coss.getUnfilteredStream();
 Document document = documentBuilder.parse(cosin);

 COSStream cosout = new COSStream(new RandomAccessBuffer());
 OutputStream out = cosout.createUnfilteredStream();

 TransformerFactory tFactory = TransformerFactory.newInstance();
 Transformer transformer = tFactory.newTransformer();
 DOMSource source = new DOMSource(xmlDoc);
 StreamResult result = new StreamResult(out);
 transformer.transform(source, result);

 PDXFA xfaout = new PDXFA(cosout);
 form.setXFA(xfaout);

这篇关于结合XFA和PDFBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆