使用iText进行Acroform编辑的Pdf
我正在使用iText将文本添加到现有的pdf文件中。它适用于简单的pdf,但与AcroForms的pdf有问题。
I am using iText for adding text to existing pdf file. It works for simple pdf but have problems with pdf with AcroForms.
我的代码:
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();
错误消息:
此文档启用了Adobe Acrobat Reader DC中的扩展功能。该文档自创建以来已更改,并且不再使用扩展功能。请联系作者以获取本文档的原始版本。
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."
并且没有我希望添加到文件的文本
and there is no text i wanted to add to file
我想要的任何想法?
您的诊断错误。 问题与AcroForms的存在无关。 问题与您的文档是否启用阅读器有关。只能使用Adobe软件完成阅读器启用。这是一个需要使用Adobe私钥进行数字签名的过程。当存在有效签名时,将在Adobe Reader中解锁特定功能(在签名时的使用权限中定义)。
Your diagnosis is wrong. The problem is not related to the presence of AcroForms. The problem is related to whether or not your document is Reader Enabled. Reader-enabling can only be done using Adobe software. It is a process that requires a digital signature using a private key from Adobe. When a valid signature is present, specific functionality (as defined in the usage rights when signing) is unlocked in Adobe Reader.
请查看此问题的答案找出如何检测PDF是否支持Reader:如何使用C#检查PDF是否启用了阅读器?
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的内容,因此您打破了签名。打破此签名是导致丑陋错误消息的原因:
You change the content of such a PDF, hence you break the signature. Breaking this signature is what causes the ugly error message:
此文档在Adobe Acrobat Reader DC中启用了扩展功能。该文档自创建以来已更改,并且不再使用扩展功能。请联系作者以获取本文档的原始版本。
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.
有两种方法可以避免此错误消息:
There are two ways to avoid this error message:
- 删除使用权利。这将导致表单不再启用Reader。例如:如果文档的创建者允许填写的表单可以在本地保存,则在删除使用权限后将无法再进行此操作。
- 以附加模式填写表单。这将导致更大的文件大小,但将保留Reader启用。
删除使用权限的方式如下:
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();
在追加模式下使用iText是这样的:
Using iText in append mode is done like this:
PdfReader reader = new PdfReader(src);
PdfStamper stamper =
new PdfStamper(reader, new FileOutputStream(dest), '\0', true);
stamper.close();
reader.close();
请注意 PdfStamper
中的额外参数。