Flatten throws exception "Object reference not set to an instance of an object"

Hi Aspose team,


  • I tried to flatten with the following code, but it throws “Object reference not set to an instance of an object”.

Document doc = new Document(filePath);
foreach (Page p in doc.Pages)
{
p.Flatten();
}
doc.Save(filePath2);

  • If I remove annotations before flatten, no exception is thrown. Could you explain me why it works in this case?

Document doc = new Document(filePath);
foreach (Page p in doc.Pages)
{
if (p.Annotations.Count > 0)
{
p.Annotations.Delete();
}
p.Flatten();
}
doc.Save(filePath2);

In addition, I know the same problem fixed in version 11.1.0, PDFNEWNET-39520 Flattening PDF file throws NullReferenceException. However, it still appears in version 11.4.0 that I’m using.

I attach the sample file.

Thanks,
Dung H. Nguyen

Hi Dung,

Thanks for your inquriy. I have tested the scenario and noticed that API is throwing NullReferenceException while flattening the PDF Pages, so logged a tikcet PDFNEWNET-40530 in our issue tracking system for further investigation. As a workaround you can flatten document instead flattening Page object as following, it will help you to accomplish the task.

Document doc = new Document(@"C:\Users\Home\Downloads\exception_flatten.pdf");

doc.Flatten();

doc.Save(myDir + "exception_flatten_flattened1.pdf");

Furthermore in reference to Aspose.Pdf for .NET 11.0.0 fix, please note it was a document specific fix.

We are sorry for the inconvenience caused.

Best Regards,

Hi Tilal Ahmad,


Thanks for your confirmation.

What should I notice if using document.flatten instead of iterating and flatten page?
Output is the same, isn’t it?

I have used page.flatten because I have to modify/verify many objects each page. It means I cannot call document.flatten in my case.

Anyway, hope we get new fix soon :d

Thanks,
Dung H. Nguyen

Hi Dung,


Thanks for your feedback. We have recorded your concern about Flatten() method of Document class and will look into it during the issue investigation and will keep you updated about the issue resolution progress.

Best Regards,

The issues you have found earlier (filed as PDFNET-40530) have been fixed in Aspose.PDF for .NET 22.3.

Not really, we have the same problem in 22.4. NullReferenceException on the Page, not on the Document
2022-05-03_19h12_06.png (56.8 KB)
Kind Regards,

@accounts.recomatics

Sometimes, the issues are related to specific PDF documents and they are resolved for them particularly. Can you please share the sample PDF with which you are facing this issue? We will test the scenario in our environment and address it accordingly.

Can i send the file in a PM, because i cannot post this file publicly.
Kind Regards,

@accounts.recomatics

Sure, you can send your file in a private message by clicking over username and pressing the Blue Message Button.