Pdf with Acroform editing using iText

2019-01-20 19:08发布

问题:

I am using iText for adding text to existing pdf file. It works for simple pdf but have problems with pdf with AcroForms.

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

and there is no text i wanted to add to file

Any ideas what I am missing?

回答1:

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.

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#?

You change the content of such a PDF, hence you break the signature. Breaking this signature is what causes the ugly 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.

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:

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.



标签: java pdf itext