使用 PDFBOX 设置的表单字段值在 Adob​​e Reader 中不可见 [英] Form field values set with PDFBOX not visible in Adobe Reader

查看:88
本文介绍了使用 PDFBOX 设置的表单字段值在 Adob​​e Reader 中不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用 Apache PDFBOX(1.8.5) 从字段设置一些时遇到问题.我有几个不同的静态 PDF 用于测试.使用以下代码,我可以设置表单字段的值,并保存生成的 PDF.然后我可以在 Adob​​e Reader 中打开此 PDF 并查看结果:

I am having an issue with trying to set some from fields using Apache PDFBOX(1.8.5). I have a few different Static PDFs that I am using for testing. Using the following code, I can set the values of form fields, and save the resulting PDF. I can then open this PDF in Adobe Reader and see the results:

PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
pdfTemplate.setAllSecurityToBeRemoved(true);
PDAcroForm acroForm = docCatalog.getAcroForm();
List fields = acroForm.getFields();     
Iterator fieldsIter = fields.iterator();        
while( fieldsIter.hasNext())
{
    PDField field = (PDField)fieldsIter.next();         
    if(field instanceof PDTextbox){
        ((PDTextbox)field).setValue("STATIC PDFBOX EDIT");
    }
}

然后我最终保存了表单.对于以下静态 PDF:

And then I eventually save the form. For Static PDFs of:

  • PDF 版本:1.6 (Acrobat 7.x)
  • PDF 版本:1.7 (Acrobat 8​​.x)

这很好用.我可以在 Adob​​e Reader XI 中打开文档并查看表单中的正确值.

This works just fine. I can open the Documents in Adobe Reader XI and see the correct values in the form.

对于以下的静态 PDF:

For Static PDFs of:

  • PDF 版本:1.7 Adob​​e Extension Level 3(Acrobat 9.x)
  • PDF 版本:1.7 Adob​​e Extension Level 8(Acrobat X)
  • PDF 版本:1.7 Adob​​e Extension Level 11(Acrobat XI)

这似乎不起作用.当我在 Adob​​e Reader XI 中打开生成的表单时,这些字段似乎没有填充.但是,如果我在 Firefox 或 Chrome 浏览器的 PDF 查看器中打开 PDF,则字段会显示为已填充.

This appears to not be working. 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.

如何设置这些字段,以便在 Adob​​e Reader XI 中查看时显示值?

How can I set these fields so the values will appear when viewed in Adobe Reader XI?

示例 PDF 可在此处找到:https://github.com/bamundson/PDFExample

Sample PDFs can be found here: https://github.com/bamundson/PDFExample

推荐答案

PDF 之间的主要区别在于所使用的表单技术:

The major difference between your PDFs is the form technology used:

  • Test_9.pdf 使用经典的 AcroForm 表单;
  • Test_10.pdfTest_10.pdf 另一方面使用具有 AcroForm 表示和 XFA 的混合形式(Adobe XML 表单架构)表示.
  • Test_9.pdf uses good ol'fashioned AcroForm forms;
  • Test_10.pdf and Test_10.pdf on the other hand use a hybrid form with both an AcroForm representation and a XFA (Adobe XML Forms Architecture) representation.

支持 XFA 的 PDF 查看器(即最重要的 Adob​​e Reader 和 Adob​​e Acrobat)使用文件中的 XFA 信息,而不支持 XFA 的查看器(即大多数其他人)使用 AcroForm> 信息.

XFA-aware PDF viewers (i.e. foremost Adobe Reader and Adobe Acrobat) use the XFA information from the file while XFA-unaware viewers (i.e. most others) use the AcroForm information.

PDFBox 大多不知道 XFA.这尤其意味着 PDAcroForm.getFields() 返回的 PDField 对象仅代表 AcroForm 信息.因此,您的 ((PDTextbox)field).setValue("STATIC PDFBOX EDIT") 调用只会影响表单的 AcroForm 表示.

PDFBox is mostly XFA-unaware. This means especially that the PDField objects returned by PDAcroForm.getFields() only represent the AcroForm information. Thus, your ((PDTextbox)field).setValue("STATIC PDFBOX EDIT") calls only influence the AcroForm representation of the form.

这解释了你的观察

当我在 Adob​​e Reader XI 中打开生成的表单时,这些字段似乎没有填充.但是,如果我在 Firefox 或 Chrome 浏览器的 PDF 查看器中打开 PDF,则字段会显示为已填充.

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.)

(As far as I know Firefox and Chrome integrated PDF viewers are XFA-unaware.)

所以,

如何设置这些字段,以便在 Adob​​e Reader XI 中查看时显示值?

How can I set these fields so the values will appear when viewed in Adobe Reader XI?

基本上有两种方式:

  1. AcroForm 字典中删除 XFA 条目:

  1. Remove the XFA entry from the AcroForm dictionary:

acroForm.setXFA(null);

如果没有 XFA,Adobe Reader 也会使用 AcroForm 表单信息.

If there is no XFA, Adobe Reader will use the AcroForm form information, too.

编辑 AcroFormXFA 信息.您可以使用

Edit both the AcroForm and the XFA information. You can retrieve the XFA information using

PDXFAResource xr = acroForm.getXFA();

并使用

xr.getDocument()

然后您可以编辑 XML,将生成的 XML 放入一个流中,您可以将其包装在 PDXFAResource 中,然后您可以使用 AcroForm.setXFA(...) 进行设置代码>.

Then you can edit the XML, put the resulting XML into a stream which you can wrap in a PDXFAResource which you then can set using AcroForm.setXFA(...).

虽然选项 1 确实更容易实现,但它仅适用于混合文档.如果您还必须编辑纯 XFA 表单,则需要实施选项 2.

While option 1 certainly is much easier to implement, it only works for hybrid documents. If you also will have to edit pure XFA forms, you'll need to implement option 2.

使用最新版本的 iText 将新字段值写入这些 PDF 可以正常工作

Writing new field values to these PDFs works fine with the latest version of iText

iText 对 XFA 表单有一定程度的明确支持.

iText has a certain degree of explicit support for XFA forms.

这篇关于使用 PDFBOX 设置的表单字段值在 Adob​​e Reader 中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆