简体   繁体   English

使用iText进行Acroform编辑的PDF

[英]Pdf with Acroform editing using iText

I am using iText for adding text to existing pdf file. 我正在使用iText将文本添加到现有的pdf文件中。 It works for simple pdf but have problems with pdf with AcroForms. 它适用于简单的pdf,但AcroForms的pdf却有问题。

My code: 我的代码:

    PdfReader reader = new PdfReader("/Users/simple-user/Downloads/acroform.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
            "/Users/simple-user/Downloads/acroform2.pdf"));
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
            BaseFont.NOT_EMBEDDED);

    PdfContentByte over = stamper.getOverContent(1);
    over.beginText();
    over.setFontAndSize(bf, 10);
    over.setTextMatrix(107, 107);
    over.showText("page updated");
    over.endText();

    stamper.close();

Error message: "This document enabled extended features in Adobe Acrobat Reader DC. The document has been changed since it was created and use of extended features is no longer available. Please contact the author for the original version of this document." 错误消息:“此文档启用了Adobe Acrobat Reader DC中的扩展功能。自创建以来,该文档已更改,不再使用扩展功能。请与作者联系以获取本文档的原始版本。”

and there is no text i wanted to add to file 而且没有我要添加到文件的文本

Any ideas what I am missing? 有什么想法我想念的吗?

Your diagnosis is wrong. 您的诊断是错误的。 The problem is not related to the presence of AcroForms. 问题与AcroForms的存在无关。 The problem is related to whether or not your document is Reader Enabled . 问题与您的文档是否启用了Reader Enabled有关 Reader-enabling can only be done using Adobe software. 启用读者功能只能使用Adobe软件完成。 It is a process that requires a digital signature using a private key from Adobe. 此过程需要使用Adobe的私钥进行数字签名。 When a valid signature is present, specific functionality (as defined in the usage rights when signing) is unlocked in Adobe Reader. 如果存在有效的签名,则会在Adobe Reader中解锁特定功能(在签名时使用权中定义)。

Please take a look at the answer to this question to find out how to detect if a PDF is Reader-enabled or not: How to Check PDF is Reader enabled or not using C#? 请查看该问题的答案,以了解如何检测PDF是否启用了Reader功能: 如何检查PDF是否启用了Reader功能,而不是使用C#?

You change the content of such a PDF, hence you break the signature. 您更改了此类PDF的内容,因此破坏了签名。 Breaking this signature is what causes the ugly error message: 破坏此签名是导致难看的错误消息的原因:

This document enabled extended features in Adobe Acrobat Reader DC. 本文档启用了Adobe Acrobat Reader DC中的扩展功能。 The document has been changed since it was created and use of extended features is no longer available. 自创建文档以来,该文档已更改,不再使用扩展功能。 Please contact the author for the original version of this document. 请与作者联系以获取本文档的原始版本。

There are two ways to avoid this error message: 有两种方法可以避免出现此错误信息:

  1. Remove the usage rights. 删除使用权。 This will result in a form that is no longer Reader enabled. 这将导致不再启用阅读器的表单。 For instance: if the creator of the document allowed that the filled out form could be saved locally, this will no longer be possible after removing the usage rights. 例如:如果文档的创建者允许将填写的表单保存在本地,则在删除使用权后将不再可能。
  2. Fill out the form in append mode. 在追加模式下填写表格。 This will result in a bigger file size, but Reader enabling will be preserved. 这将导致更大的文件大小,但是将保留启用阅读器的功能。

Removing usage rights is done like this: 删除使用权是这样完成的:

PdfReader reader = new PdfReader(path_to_file);
if (reader.hasUsageRights()) {
    reader.removeUsageRights();
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(path_to_new_file));
    stamper.close();
}
reader.close();

Using iText in append mode is done like this: 可以通过以下方式在附加模式下使用iText:

PdfReader reader = new PdfReader(src);
PdfStamper stamper =
    new PdfStamper(reader, new FileOutputStream(dest), '\0', true);
stamper.close();
reader.close();

Note the extra parameters in PdfStamper . 注意PdfStamper的额外参数。

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

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