java 解析pdf 表单,Java pdfBox:填写pdf表单,将其附加到pddocument,然后重复

I have a pdf form made and I'm trying to use pdfBox to fill in the form and print the document. I got it working great for 1 page print jobs but i had to try and modify for multiple pages. Basically it's a form with basic info up top and a list of contents. Well if the contents are larger than what the form has room for I have to make it a multiple page document. I end up with a document with a nice page one and then all the remaining pages are the blank template. What am I doing wrong?

PDDocument finalDoc = new PDDocument();

File template = new File("path/to/template.pdf");

//Declare basic info to be put on every page

String name = "John Smith";

String phoneNum = "555-555-5555";

//Get list of contents for each page

List>> pageContents = methodThatReturnsMyInfo();

for (List> content : pageContents) {

PDDocument doc = new PDDocument().load(template);

PDDocumentCatlog docCatalog = doc.getDocumentCatalog();

PDAcroForm acroForm = docCatalog.getAcroForm();

acroForm.getField("name").setValue(name);

acroForm.getField("phoneNum").setValue(phoneNum);

for (int i=0; i

acroForm.getField("qty"+i).setValue(content.get(i).get("qty"));

acroForm.getField("desc"+i).setValue(content.get(i).get("desc"));

}

List pages = docCatalog.getAllPages();

finalDoc.addPage(pages.get(0));

}

//Then prints/saves finalDoc

解决方案

There are two major issues in you code:

The AcroForm element of a PDF is a document level object. You only copy the filled-in template page into finalDoc. Thus, the form fields are added to finalDoc only as annotations of their respective page but they are not added to the AcroForm of finalDoc.

This is not apparent in Adobe Reader but form filling services often identify available fields from the document level AcroForm entry and don't search the pages for additional form fields.

The actual show stopper: You add fields with identical names to the PDF. But PDF forms are document-wide entities. I.e. there can be only a single field entity with a given name in a PDF. (This field entity may have multiple visualizations aka widgets but this requires you to construct a single field object with multiple kid widgets.Furthermore these widgets are expected to display the same value which is not what you want...)

Thus, you have to rename the fields uniquely before adding them to the finalDoc.

Here a simplified example which works on a template with only one field "SampleField":

byte[] template = generateSimpleTemplate();

Files.write(new File(RESULT_FOLDER, "template.pdf").toPath(), template);

try ( PDDocument finalDoc = new PDDocument(); )

{

List fields = new ArrayList();

int i = 0;

for (String value : new String[]{"eins", "zwei"})

{

PDDocument doc = new PDDocument().load(new ByteArrayInputStream(template));

PDDocumentCatalog docCatalog = doc.getDocumentCatalog();

PDAcroForm acroForm = docCatalog.getAcroForm();

PDField field = acroForm.getField("SampleField");

field.setValue(value);

field.setPartialName("SampleField" + i++);

List pages = docCatalog.getAllPages();

finalDoc.addPage(pages.get(0));

fields.add(field);

}

PDAcroForm finalForm = new PDAcroForm(finalDoc);

finalDoc.getDocumentCatalog().setAcroForm(finalForm);

finalForm.setFields(fields);

finalDoc.save(new File(RESULT_FOLDER, "form-two-templates.pdf"));

}

As you see all fields are renamed before they are added to finalForm:

field.setPartialName("SampleField" + i++);

and they are collected in the list fields which finally is added to the finalForm AcroForm:

fields.add(field);

}

...

finalForm.setFields(fields);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值