Hello,
I’ve noticed that document.getPageCount() alters the document itself - it adds the page count number to the document:
Aspose.Words v24.12
Hello,
I’ve noticed that document.getPageCount() alters the document itself - it adds the page count number to the document:
The document (of type Document) is generated by aspose, I took the document.toString() value before and after document.getPageCount() operation and got the extra number there.
(I add more data to the document afterwards (using tables), and get an error “java.lang.IllegalStateException: The character index is too large.”, I assume some table is not closed properly and it happens due to this addition)
The document looks like this:
Doc Report22_06_2025(6).docx (15.8 KB)
@elena4958 Your document has a PageNum field, which is in the footer of the document, and this is the expected behavior. The last page is 3, so you see 3 in the output. This doesn’t happen only when using document.getPageCount()
, but I can get this number with the following code:
Document doc = new Document("input.docx");
System.out.println(doc.toString(SaveFormat.TEXT));
That’s right, I have the page numbers, but why there is a difference before and after the getPageCount()?
@elena4958 Unfortunately, on my side I always have the same output with a value of 3. Could you please create a simple application to help us reproduce the problem.
Perhaps it happens as your check happens on a ready document, and mine - during the document generation. I have tables and styling in the document, and, unfortunately, I failed to reproduce it in a simple snippet.
@elena4958 Thank you for providing additional information. In this case, the fields were not updated when the documents were generated by default, so you did not observe the field value in the information. However, when you use getPageCount()
, you need to build the layout and update the fields (similar to updateFields()
) in the document, which is the reason for this output. Meanwhile, this is not a bug, but expected behavior.
Thank you, do you mean I should perform doc.updateFields() before taking getPageCount()? If so, for me the result is the same (with extra page number in the end), so, unfortunately it doesn’t solve the problem.
@elena4958 When you access Document.PageCount
property Aspose.Words internally calls Document.UpdatePagePayout()
method, which leads to calling doc.UpdateFields()
. If you need to get the result without the page number, you need to get the results excluding fields or Footer nodes where these fields are located.