2

Got some legacy code using iTextSharp.

All documents have GenerateAppearances set to true regardless. and now its triggering an exception.

Took the basic code out and placed it into a Console app, same thing, Used a generic PDF (http://www.pdf995.com/samples/pdf.pdf) of the net same thing.

This is using version 5.5.12

class Program
{
    static void Main(string[] args)
    {
        var reader = new PdfReader(@"C:\Users\me\Desktop\pdf.pdf"); 

        var outStream = new MemoryStream();
        var stamper = new PdfStamper(reader, outStream);

        stamper.AcroFields.GenerateAppearances = true; <--- usually true before setting
        stamper.FormFlattening = true;
    }
}

An unhandled exception of type 'System.NullReferenceException' occurred in itextsharp.dll

Additional information: Object reference not set to an instance of an object.

Thanks

  • Define legacy code. Are you talking about an iTextSharp version that is several years old? In that case, the question risks to be closed for the following reason: "This question was caused by a problem that can no longer be reproduced." – Bruno Lowagie Nov 08 '17 at 12:23
  • This example happens with the latest version - 5.5.12. – Michael Kearney Nov 08 '17 at 12:34
  • OK, in that case it surprises me that you have to set `GenerateAppearances` to `true`. Have you checked if `stamper.AcroFields` is not `null`? It is normal that you get a `NullReferenceException` in case you process a document that isn't an AcroForm (it may look like a form to the human eye, but that doesn't mean it's a form to a machine). – Bruno Lowagie Nov 08 '17 at 12:48
  • AcroFields is not null. GenerateAppearances is true all before assigning true again. Triggers an exception: System.NullReferenceException.Data: ListDictionaryInternal Count is 0 – Michael Kearney Nov 08 '17 at 12:52

2 Answers2

1

After years of production, I just changed it from:

if (stamper.AcroFields != null)
{
    f.GenerateAppearances = true;

    foreach(var field in f.Fields)
    {
        f.SetField(field.Key, f.GetField(field.Key));
    }

    stamper.FormFlattening = true;
}

to

if (stamper.AcroFields != null && stamper.AcroFields.GenerateAppearances == true)
Saeid Amini
  • 1,313
  • 5
  • 16
  • 26
0

I encounter the same issues as below: When I set AcroFields.GenerateAppearances = true an unhandled exception of type 'System.NullReferenceException' occurred in itextsharp.dll Additional information: Object reference not set to an instance of an object.

I debugged this code and found that AcroFields is not null. but NullReferenceException still occourred.

After inverstigating, I found that the format of PDF file is aspose xfa rather than acroforms. So I solved this issue by converting the format of PDF from aspose xfa to acroforms.

  • What do you mean by "*aspose* xfa"? XFA is a semi-proprietary *Adobe* format... – mkl Jan 10 '22 at 09:06
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '22 at 09:33