Aspose.PDF gets System.NullReferenceException attempting to flatten a document

I’m using Aspose.PDF.Drawing version 25.11. I’m using this version instead of Aspose.PDF because I want to run in Linux.

Our customer has a bunch of documents that were created by a program called Bluebeam Brewery 5.0 from application Bluebean Stapler 2019.1.20.129 and they contain some null pages when I load them into Aspose and that is causing flattening to fail with exception “Object reference not set to an instance of an object”. I have sample document that I can include where page 9 is null. If I make a new document and remove the null page then it works. I’m not sure why the page is null but is there a way to prevent Aspose from getting the exception? Seems like it could just check and skip any null pages. I also tried validate and repair functions but they also get this same excception.

DG00000487.PDF (5.8 MB)

@skowald

What you observed

  • When you call document.Flatten() on the PDF that contains a null page (page 9 in the sample you attached), a System.NullReferenceException is thrown.
  • The same exception is also raised by the Validate() and Repair() methods, because they iterate over the page collection in the same way.

These symptoms match the behavior you described in the forum post.

Why the exception occurs

The library expects every entry in Document.Pages to be a valid Page object. If a null entry is encountered, the internal code dereferences it, which results in the NullReferenceException you see. At present there is no documented option or flag in Aspose.PDF.Drawing 25.11 to tell the API to ignore such null pages during flattening, validation, or repair.

Possible workaround

You can remove the null entries from the page collection before invoking the operations that fail. The following code demonstrates one way to do this on Linux with Aspose.PDF.Drawing 25.11:

using Aspose.Pdf.Drawing;

// Load the PDF (from file, stream, etc.)
var doc = new Document("DG00000487.PDF");

// Iterate backwards so that removal does not affect the index of remaining pages
for (int i = doc.Pages.Count; i >= 1; i--)
{
    var page = doc.Pages[i];
    if (page == null)               // Null page found – remove it
        doc.Pages.Delete(i);
}

// Now the operations should succeed
doc.Flatten();          // or doc.Validate(); doc.Repair();
doc.Save("DG00000487_flattened.pdf");

Next steps

  • Since the exception is caused by a null page in the source PDF, the above manual removal is a practical way to proceed.
  • If you need a permanent fix or further assistance, consider opening a support ticket with Aspose, providing the sample PDF and the version you are using, so the product team can investigate the issue.