Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable extended features with iTextSharp

Tags:

c#

itextsharp

I have a PDF template with a form with the Extended features enabled. After filling in the fields of this form using iTextSharp, a user with acrobat reader gets the error message:

This document enabled extended features in Adobe Reader. 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.

I googled a bit but all the posts talk about "enabling" extended features, however, I want the form fields to remain disabled and extended features turned off

Here a sample code which I am using:

using (var existingFileStream = new FileStream(fileNameExisting, FileMode.Open))
using (var newFileStream = new FileStream(fileNameNew, FileMode.Create))
{
    // Open existing PDF
    var pdfReader = new PdfReader(existingFileStream);

    // PdfStamper, which will create
    var stamper = new PdfStamper(pdfReader, newFileStream);

    var form = stamper.AcroFields;

    var fieldKeys = form.Fields.Keys;

    foreach (string fieldKey in fieldKeys)
    {
        if (fieldKey.Equals("Retailer Name"))
            form.SetField(fieldKey, retailerName);
    }
    // “Flatten” the form so it wont be editable/usable anymore
    stamper.FormFlattening = true;

    stamper.Close();
    pdfReader.Close();
}
like image 453
Jags Avatar asked Jul 25 '13 08:07

Jags


2 Answers

The links here are dead as the iTextPdf web site has been completely revamped. But the answer can be understood without those links, too.

The iText Keyword: Reader enabled PDFs points to the following information:

Submitted by Bruno Lowagie on Fri, 12/31/2010 - 16:37

After filling out my form, my PDF shows the following message: This document enabled extended features in Adobe Reader. 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. How do I avoid this message?

The creator of the form made the document Reader enabled. Reader enabling can only be done using Adobe software. You can avoid this message in two ways:

  • 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.
  • Fill out the form in append mode. This will result in a bigger file size, but Reader enabling will be preserved.

It also points to the sample ReaderEnabledForm.java (the C#/iTextSharp equivalent of which is ReaderEnabledForm.cs) which shows how to do either.

In your case this amounts to calling

pdfReader.RemoveUsageRights();

right after creating the PdfReader and before creating the PdfStamper.

/**
 * Removes any usage rights that this PDF may have. Only Adobe can grant usage rights
 * and any PDF modification with iText will invalidate them. Invalidated usage rights may
 * confuse Acrobat and it's advisabe to remove them altogether.
 */
public void RemoveUsageRights()
like image 107
mkl Avatar answered Oct 15 '22 21:10

mkl


Fill out the form in append mode by using the PdfStamper constractor overload

// PdfStamper, which will create
var stamper = new PdfStamper(pdfReader, fileStream, '\0', true);
like image 45
Nerdroid Avatar answered Oct 15 '22 21:10

Nerdroid