Accessing Document.PageCount Changes Rendering Output

Description

Accessing the read-only property Document.PageCount changes the subsequent image rendering output of the same document.
The document renders correctly before reading PageCount, but the layout/alignment changes after it is accessed.

Code Sample

using Aspose.Words;

new License().SetLicense("licensePath");
var doc = new Document("inputDocxPath");

// Image output #1
doc.Save("beforePageCountOutputPngPath");

// Only read PageCount
_ = doc.PageCount;

// Image output #2
doc.Save("afterPageCountOutputPngPath");

Reproduction Steps

  1. Load the DOCX
  2. Save to PNG
  3. Read doc.PageCount
  4. Save to PNG again

Sample Docx

sample.docx (16.5 KB)

Result

Before reading PageCount

After reading PageCount

Expected Result

Accessing Document.PageCount should be read-only and must not affect layout or rendering.
Both image outputs should be identical.
The first rendering (before reading PageCount) is the correct result.

Environment

  • Aspose.Words for .NET 25.12.0
  • .NET Framework 4.8

Question

  • Why does reading Document.PageCount affect rendering?
  • Does it internally trigger a layout rebuild (e.g. UpdatePageLayout())?

@chenweii

What happens when Document.PageCount is accessed
Reading Document.PageCount forces Aspose.Words to perform a pagination pass. Internally the library invokes Document.UpdatePageLayout() to calculate the number of pages.

Why the rendering output changes
The pagination pass recomputes the layout of the whole document (including floating objects, tables, etc.). Because the layout is rebuilt after the first PNG is generated, the second rendering uses a newly‑computed layout snapshot, which can lead to visual differences such as the alignment shift you observed [0].

Is this expected?
In a stable layout the visual result should be identical before and after obtaining the page count. The change you see is not intended and is a regression in Aspose.Words for .NET 25.12.0.

Work‑around

var doc = new Document("input.docx");

// Force a stable layout once
doc.UpdatePageLayout();          // optional but guarantees layout is fixed

// First render
doc.Save("first.png");

// Retrieve page count only after rendering (or store it before the first render)
int pageCount = doc.PageCount;

// Subsequent renders will use the same layout
doc.Save("second.png");

Calling UpdatePageLayout() before any rendering (or retrieving the page count only after all rendering is finished) prevents the layout from being rebuilt between renders.

Next steps
We have logged this behavior as a bug for the Aspose.Words for .NET 25.12.0 release. Our development team will investigate and provide a fix in a future update. If you need an immediate resolution, consider using the workaround above or upgrading to a newer version when it becomes available.

@chenweii
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28940

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.