我已经搜索了很多,但找不到合适的答案。我正在使用PDFBox库解决方案,在那里我有一个预定义的pdf表单模板。这个模板是在奥多比InDesign设计的,给了我一个可编辑的pdf格式。
我的目标是以编程方式填充此表单,其中的数据将来自其他一些apis。我可以通过设置PDAcroForm
对象的适当字段来填充模板的固定部分(非重复信息)。这工作得很好,我能够正确地查看填充的pdf。
此模板表单中有一个表,行数可以根据数据动态增加。我不能填充这个表,或者你可以说我不知道如何在模板中创建动态行。你能建议一下如何在表格模板中填写表格吗?提前谢谢。
下面是我写的代码:
PDDocument pdfDocument = PDDocument.load(new ClassPathResource("sample_template.pdf").getFile());
PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
acroForm.getField("Field1).setValue("Hello");
acroForm.getField("Field2).setValue("World");
.
.
for (Order eachOrder : Orders) {
// Here I want to fill a table with the data.
// Number of columns are fixed but rows are dynamic.
acroForm.getField("OrderDate").setValue(eachOrder.getDate());
acroForm.getField("OrderDesc").setValue(eachOrder.getDescription());
}
// The problem with above loop is that it does not creates new rows but it
// keeps on overwriting the first row (which is obvious because in my template there is only 1 row defined with these 2 columns).
// I don't know how to define a variable row in template and fill it with the data.
.
.
acroForm.flatten();
pdfDocument.save("test.pdf");
请建议如何从此处继续。
发布于 2021-11-18 09:54:39
该文件是XFA PDF,您需要对其进行相应的处理。基本上,您有一个包含XML的字段,其中包含字段值。您的PDAcroForm对象上有一个getXFA()方法。
有关XFA的简短描述可在以下位置找到:https://blog.idrsolutions.com/2014/03/extracting-data-xfa-files/
https://stackoverflow.com/questions/68142481
复制相似问题