如何解决如何在pdf表单模板中使用PDFBox动态填充表格
我搜索了很多,但找不到合适的答案来解决我的问题。我正在使用 PDFBox 库开发一个解决方案,其中我有一个预定义的 pdf 表单模板。此模板是在 Adobe InDesign 中设计的,并以可编辑的 pdf 表单形式提供给我。
我的目标是以编程方式填写此表单,其中数据将来自其他一些 API。我可以通过设置 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");
请建议如何从这里开始。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。