问题描述
我搜索了很多,但找不到合适的答案来解决我的问题。我正在使用 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 (将#修改为@)