简体   繁体   English

使用PDF框设置的表单字段值在Adobe Reader中不可见

[英]Form field values set with PDFBOX not visible in Adobe Reader

I am having an issue with trying to set some from fields using Apache PDFBOX(1.8.5). 尝试使用Apache PDFBOX(1.8.5)从字段中设置一些问题我遇到了问题。 I have a few different Static PDFs that I am using for testing. 我有几个不同的静态PDF用于测试。 Using the following code, I can set the values of form fields, and save the resulting PDF. 使用以下代码,我可以设置表单字段的值,并保存生成的PDF。 I can then open this PDF in Adobe Reader and see the results: 然后,我可以在Adobe Reader中打开此PDF并查看结果:

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");
    }
}

And then I eventually save the form. 然后我最终保存了表格。 For Static PDFs of: 对于静态PDF:

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

This works just fine. 这很好用。 I can open the Documents in Adobe Reader XI and see the correct values in the form. 我可以在Adobe Reader XI中打开文档,并在表单中查看正确的值。

For Static PDFs of: 对于静态PDF:

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

This appears to not be working. 这似乎不起作用。 When I open the resulting forms in Adobe Reader XI, the fields do not appear to be populated. 当我在Adobe Reader XI中打开生成的表单时,字段似乎不会填充。 But If I open the PDF in my Firefox or Chrome browser's PDF viewer, the fields show as populated there. 但是,如果我在我的Firefox或Chrome浏览器的PDF查看器中打开PDF,则字段显示为填充在那里。

How can I set these fields so the values will appear when viewed in Adobe Reader XI? 如何设置这些字段,以便在Adobe Reader XI中查看时显示值?

EDIT: Sample PDFs can be found here: https://github.com/bamundson/PDFExample 编辑:示例PDF可以在这里找到: https//github.com/bamundson/PDFExample

The major difference between your PDFs is the form technology used: 您的PDF之间的主要区别在于使用的表单技术:

  • Test_9.pdf uses good ol'fashioned AcroForm forms; Test_9.pdf使用了良好的ol'fashioned AcroForm形式;
  • 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. Test_10.pdfTest_10.pdfTest_10.pdf使用具有AcroForm表示和XFA (Adobe XML Forms Architecture)表示的混合形式。

XFA-aware PDF viewers (ie foremost Adobe Reader and Adobe Acrobat) use the XFA information from the file while XFA-unaware viewers (ie most others) use the AcroForm information. 支持XFA的PDF查看器(即最重要的Adobe Reader和Adobe Acrobat)使用文件中的XFA信息,而XFA不知情的查看者(即大多数其他人)使用AcroForm信息。

PDFBox is mostly XFA-unaware. PDFBox主要是XFA不知道的。 This means especially that the PDField objects returned by PDAcroForm.getFields() only represent the AcroForm information. 这尤其意味着PDAcroForm.getFields()返回的PDField对象仅代表AcroForm信息。 Thus, your ((PDTextbox)field).setValue("STATIC PDFBOX EDIT") calls only influence the AcroForm representation of the form. 因此,您的((PDTextbox)field).setValue("STATIC PDFBOX EDIT")调用仅影响表单的AcroForm表示。

This explains your observation 这解释了你的观察

When I open the resulting forms in Adobe Reader XI, the fields do not appear to be populated. 当我在Adobe Reader XI中打开生成的表单时,字段似乎不会填充。 But If I open the PDF in my Firefox or Chrome browser's PDF viewer, the fields show as populated there. 但是,如果我在我的Firefox或Chrome浏览器的PDF查看器中打开PDF,则字段显示为填充在那里。

(As far as I know Firefox and Chrome integrated PDF viewers are XFA-unaware.) (据我所知,Firefox和Chrome集成的PDF查看器是XFA不知道的。)

So, 所以,

How can I set these fields so the values will appear when viewed in Adobe Reader XI? 如何设置这些字段,以便在Adobe Reader XI中查看时显示值?

There essentially are two ways: 基本上有两种方式:

  1. Remove the XFA entry from the AcroForm dictionary: AcroForm字典中删除XFA条目:

     acroForm.setXFA(null); 

    If there is no XFA , Adobe Reader will use the AcroForm form information, too. 如果没有XFA ,Adobe Reader也将使用AcroForm表单信息。

  2. Edit both the AcroForm and the XFA information. 编辑AcroFormXFA信息。 You can retrieve the XFA information using 您可以使用检索XFA信息

     PDXFAResource xr = acroForm.getXFA(); 

    and extract the underlying XML using 并使用提取底层XML

     xr.getDocument() 

    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(...) . 然后,您可以编辑XML,将生成的XML放入流中,您可以将其包装在PDXFAResource ,然后可以使用AcroForm.setXFA(...)

While option 1 certainly is much easier to implement, it only works for hybrid documents. 虽然选项1当然更容易实现,但它仅适用于混合文档。 If you also will have to edit pure XFA forms, you'll need to implement option 2. 如果您还需要编辑纯XFA表单,则需要实现选项2。

Writing new field values to these PDFs works fine with the latest version of iText 使用最新版本的iText可以为这些PDF写入新的字段值

iText has a certain degree of explicit support for XFA forms. iText对XFA表单有一定程度的明确支持。

You need to set NeedAppearances flag to true for acrofields. 您需要为acrofields设置NeedAppearances标志为true。 Try following code snippet. 请尝试以下代码段。 It will display values in fields properly. 它将正确显示字段中的值。

COSDictionary acroFormDict = new COSDictionary(); 
acroFormDict.setBoolean(COSName.getPDFName("NeedAppearances"), true);
acroFormDict.setItem(COSName.getPDFName("Fields"), new COSArray());

// add a new AcroForm and add that to the document
PDAcroForm acroForm = new PDAcroForm(document, acroFormDict);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM