为什么在不调用flattenFields方法的情况下将表单展平? [英] Why is my form being flattened without calling the flattenFields method?

查看:314
本文介绍了为什么在不调用flattenFields方法的情况下将表单展平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下格式测试我的方法 https://help .adobe.com/zh_CN/Acrobat/9.0/Samples/interactiveform_enabled.pdf

I am testing my method with this form https://help.adobe.com/en_US/Acrobat/9.0/Samples/interactiveform_enabled.pdf

它被这样称呼:

Pdf.editForm("./src/main/resources/pdfs/interactiveform_enabled.pdf", "./src/main/resources/pdfs/FILLEDOUT.pdf"));

其中Pdf只是一个工作程序类,而editForm是一个静态方法.

where Pdf is just a worker class and editForm is a static method.

editForm方法如下所示:

public static int editForm(String inputPath, String outputPath) {
    try {
        PdfDocument pdf = new PdfDocument(new PdfReader(inputPath), new PdfWriter(outputPath));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
        Map<String, PdfFormField> m = form.getFormFields();
        for (String s : m.keySet()) {
            if (s.equals("Name_First")) {
                m.get(s).setValue("Tristan");
            }
            if (s.equals("BACHELORS DEGREE")) {
                m.get(s).setValue("Off"); // On or Off
            }
            if (s.equals("Sex")) {
                m.get(s).setValue("FEMALE");
            }
            System.out.println(s);
        }
        pdf.close();
        logger.info("Completed");
    } catch (IOException e) {
        logger.error("Unable to fill form " + outputPath + "\n\t" + e);
        return 1;
    }

    return 0;
}

不幸的是,调用此方法后,FILLEDOUT.pdf文件不再是表单.我在做错什么吗?

Unfortunately the FILLEDOUT.pdf file is no longer a form after calling this method. Am I doing something wrong?

我正在使用资源以进行指导.请注意我如何不调用form.flattenFields().但是,如果我调用该方法,则会出现错误java.lang.IllegalArgumentException.

I was using this resource for guidance. Notice how I am not calling the form.flattenFields(). If I do call that method however, I get an error of java.lang.IllegalArgumentException.

谢谢您的时间.

推荐答案

您的表单已启用读者功能,即该表单包含由Adobe发行的密钥和证书向普通Adobe Reader指示使用权的数字使用权签名在该PDF上进行操作时,会激活许多其他功能.

Your form is Reader-enabled, i.e. it contains a usage rights digital signature by a key and certificate issued by Adobe to indicate to a regular Adobe Reader that it shall activate a number of additional features when operating on that very PDF.

如果按照原始代码标记文件,则现有的PDF对象将被重新排列并稍作更改.这破坏了使用权签名,并且Adobe Reader意识到,它否认自创建文档以来就对其进行了更改,并且不再可以使用扩展功能."

If you stamp the file as in your original code, the existing PDF objects will get re-arranged and slightly changed. This breaks the usage rights signature, and Adobe Reader, recognizing that, disclaims "The document has been changed since it was created and use of extended features is no longer available."

但是,如果以追加模式标记文件,则更改将作为增量更新附加到PDF.因此,签名仍然可以正确签名其原始字节范围,并且Adobe Reader不会抱怨.

If you stamp the file in append mode, though, the changes are appended to the PDF as an incremental update. Thus, the signature still correctly signs its original byte range and Adobe Reader does not complain.

要激活追加模式,请在创建PdfDocument时使用StampingProperties:

To activate append mode, use StampingProperties when you create your PdfDocument:

PdfDocument pdf = new PdfDocument(new PdfReader(inputPath), new PdfWriter(outputPath), new StampingProperties().useAppendMode());

(已通过iText 7.1.1-SNAPSHOT和Adobe Acrobat Reader DC版本2018.009.20050测试)

(Tested with iText 7.1.1-SNAPSHOT and Adobe Acrobat Reader DC version 2018.009.20050)

顺便说一句,Adobe Reader不仅检查签名,还尝试确定增量更新中的更改是否不超出使用权利签名激活的附加功能的范围.

By the way, Adobe Reader does not merely check the signature, it also tries to determine whether the changes in the incremental update don't go beyond the scope of the additional features activated by the usage rights signature.

否则,您可以简单地获取一个小型的启用Reader的PDF,并在附加模式下用您自己选择的内容替换所有现有页面.这当然不符合Adobe的利益...

Otherwise you could simply take a small Reader-enabled PDF and in append mode replace all existing pages by your own content of choice. This of course is not in Adobe's interest...

这篇关于为什么在不调用flattenFields方法的情况下将表单展平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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