您的PDF之间的主要区别是使用的表格技术:
> Test_9.pdf使用好的ol’fashioned AcroForm表单;
> Test_10.pdf和Test_10.pdf另一方面使用具有AcroForm表示和XFA(Adobe XML Forms Architecture)表示形式的混合形式.
XFA感知的PDF浏览者(即最重要的Adobe Reader和Adobe Acrobat)在XFA不知情的观众(即大多数其他人)使用AcroForm信息时使用该文件中的XFA信息.
PDFBox主要是XFA不知道.这意味着特别是PDAcroForm.getFields()返回的PDField对象只表示AcroForm信息.因此,您的((PDTextBox)字段).setValue(“STATIC PDFBox EDIT”)调用仅影响表单的AcroForm表示.
这解释了你的观察
When I open the resulting forms in Adobe Reader XI,the fields do not appear to be populated. But If I open the PDF in my Firefox or Chrome browser’s PDF viewer,the fields show as populated there.
(据我所知,Firefox和Chrome集成PDF查看器是XFA不知道的.)
所以,
How can I set these fields so the values will appear when viewed in Adobe Reader XI?
基本上有两种方式:
>从AcroForm字典中删除XFA条目:
acroForm.setXFA(null);
如果没有XFA,Adobe Reader也会使用AcroForm表单信息.
>编辑AcroForm和XFA信息.您可以使用XFA信息检索
PDXFAResource xr = acroForm.getXFA();
并使用底层XML提取
xr.getDocument()
然后,您可以编辑XML,将生成的XML放入一个可以包装在PDXFAResource中的流中,然后您可以使用AcroForm.setXFA(…)进行设置.
而选项1肯定更容易实现,它只适用于混合文档.如果您还必须编辑纯XFA表单,则需要实现选项2.
Writing new field values to these PDFs works fine with the latest version of iText
iText对XFA表单有一定程度的明确支持.