Disable extended features with iTextSharp

北城余情 提交于 2019-12-05 19:37:25

问题


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();
}

回答1:


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()



回答2:


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);


来源:https://stackoverflow.com/questions/17852902/disable-extended-features-with-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!