我想用PDFBox java库填一张PDF表格。PDF表单是使用Adobe Live Designer创建的,因此它使用XFA格式。
我试图找到关于用PDFBox填充XFA表单的参考资料,但是到目前为止我还没有任何收获。我看到PDAcroForm.setXFA方法在API中可用,但我不知道如何使用它。
您是否知道是否可以使用PDFBox填写PDF表单?如果是,有没有代码示例或教程来实现这一点?如果没有,那么实现这一目标的最佳替代方案是什么?
发布于 2014-11-29 03:32:09
这是在分配给我解决这个问题的时间内我所能做到的最好的事情。我将pdf保存(在生命周期中)作为优化(我不是做pdf的那个人)。这是PDF的开篇部分,XML复制然后保存:
PDDocument document = PDDocument.load(fileInputStream);
fileInputStream.close();
document.setAllSecurityToBeRemoved(true);
Map<String, String> values = new HashMap<String, String>();
values.put("variable_name", "value");
setFields(document, values); // see code below
PDAcroForm form = document.getDocumentCatalog().getAcroForm();
Document documentXML = form.getXFA().getDocument();
NodeList dataElements = documentXML.getElementsByTagName("xfa:data");
if (dataElements != null) {
for (int i = 0; i < dataElements.getLength(); i++) {
setXFAFields(dataElements.item(i), values);
}
}
COSStream cosout = new COSStream(new RandomAccessBuffer());
TransformerFactory.newInstance().newTransformer()
.transform(new DOMSource(documentXML), new StreamResult(cosout.createUnfilteredStream()));
form.setXFA(new PDXFA(cosout));
FileOutputStream fios = new FileOutputStream(new File(docOut + ".pdf"));
document.save(fios);
document.close();
try {
fios.flush();
} finally {
fios.close();
}然后是为字段设置值的方法。我同时设置了XFA和AcroForm:
public void setXFAFields(Node pNode, Map<String, String> values) throws IOException {
if (values.containsKey(pNode.getNodeName())) {
pNode.setTextContent(values.get(pNode.getNodeName()));
} else {
NodeList childNodes = pNode.getChildNodes();
if (childNodes != null) {
for (int i = 0; i < childNodes.getLength(); i++) {
setXFAFields(childNodes.item(i), values);
}
}
}
}
public void setFields(PDDocument pdfDocument, Map<String, String> values) throws IOException {
@SuppressWarnings("unchecked")
List<PDField> fields = pdfDocument.getDocumentCatalog().getAcroForm().getFields();
for (PDField pdField : fields) {
setFields(pdField, values);
}
}
private void setFields(PDField field, Map<String, String> values) throws IOException {
List<COSObjectable> kids = field.getKids();
if (kids != null) {
for (COSObjectable pdfObj : kids) {
if (pdfObj instanceof PDField) {
setFields((PDField) pdfObj, values);
}
}
} else {
// remove the [0] from the name to match values in our map
String partialName = field.getPartialName().replaceAll("\\[\\d\\]", "");
if (!(field instanceof PDSignatureField) && values.containsKey(partialName)) {
field.setValue(values.get(partialName));
}
}
}这项工作,但不是所有“种类”的PDF生命周期产生,一些人得到了一个警告消息“扩展功能”不再启用,但仍然工作。优化版本是我发现的唯一一个在填写后打开时不提示消息的版本。
我填充了XFA和Acroform,否则它不能在所有查看器中工作。
发布于 2014-01-17 01:47:13
这个问题特别指出了主题中的PDFBox库;您不需要iText,可以使用PDXFA1.8中提供的PDFBox对象来完成XFA操作。
非常感谢Maruan Sahyoun在PDFBox + XFA方面所做的出色工作。
仅当您删除PDDocument上的所有安全性时,此代码才有效。
它还假设PDXFA中的COS对象是一个COSStream。下面这个简单的示例读取xml流并将其写回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);发布于 2012-05-17 03:20:25
我不熟悉pdfbox,但是一旦访问了XFA (XML) DOM,就可以用iText (http://itextpdf.com/)做到这一点。
https://stackoverflow.com/questions/10536334
复制相似问题