We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

UpdatePageLayout throws NullReferenceException using C#

Hello,
I have a complex code with a complex template that in some conditions throws a NullReferenceException exception when saving as PDF.
It required me a lot of time to understand what caused this exception in my code because it did not happen always, but at the end I found a way to reproduce the bug in an easy way.

All these conditions must be met to see the bug:

  1. the document must have a page number field
  2. the method Document.UpdateFields() must be called before editing the document
  3. the document must be edited
  4. the method Document.UpdateFields() must NOT be called after editing the document
  5. the document must be saved as PDF

If also only one of the condtions is changed, the exception is not thrown.

Note:
I’m not blocked by this bug because I do not need to call Document.UpdateFields() in my real case (it was an error in my code to do so), anyway I wanted to notify this to you as this could potentially happen in other contexts.

Tested with Aspose.Words 20.3 and Word 2013.

Attached you can find an example document with C# code.
Bug Dynamic Columns with UpdateFields.zip (14.1 KB)

@agarberi

When you edit the document and call Document.UpdateFields method, we suggest you please call Document.UpdatePageLayout method to avoid the exception. Hope this helps you.

We called this method before saving the document to PDF and have not faced the shared exception.

document.UpdatePageLayout();
document.Save(MyDir + "output.pdf");