getPageCount is not correct

test.docx (60.0 KB)

the word shows 1 page to me, but I used the following code, the page count is 2.

Document doc = new Document(path + "test.docx");
int c = doc.getPageCount();

@GusGus

It seems that you are experiencing an issue where the page count of your Word document is reported as 2, while you expect it to be 1. This discrepancy can occur due to several reasons related to how Aspose.Words calculates the page count.

The getPageCount method retrieves the number of pages based on the most recent layout operation. If the document has not been laid out recently, or if there are elements in the document that affect the layout (such as page breaks, section breaks, or other formatting), it may lead to an incorrect page count.

To ensure that you get the correct page count, you can call the UpdatePageLayout method before retrieving the page count. This method forces the document to recalculate its layout, which should provide you with an accurate page count. Here’s how you can modify your code:

Document doc = new Document(path + "test.docx");

// Update the page layout to ensure accurate page count
doc.updatePageLayout();

int c = doc.getPageCount();
System.out.println("Page count: " + c);

By calling updatePageLayout(), you ensure that the document’s layout is up-to-date, which should resolve the issue of the incorrect page count. If the problem persists, you may want to check the document for any hidden elements or formatting that could be affecting the layout.

For more detailed information on the PageCount property and how it works, you can refer to the official documentation here.

If you continue to face issues, please provide more details about the document’s content or any specific formatting that might be influencing the page count.

@GusGus Most likely the problem occurs because the fonts used in the document are not available in the environment where document is rendered to PDF. If Aspose.Words cannot find the fonts used in the document the fonts are substituted . This might lead into the layout differences due to differences in fonts metrics. You can implement IWarningCallback to get a notification when font substitution is performed.
The following articles can be useful for you:
https://docs.aspose.com/words/java/specify-truetype-fonts-location/
https://docs.aspose.com/words/java/install-truetype-fonts-on-linux/

In addition, to get correct rendering your document it is required to enable open type features. You should be install the above package

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>25.2</version>
    <classifier>jdk17</classifier>
</dependency>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>25.2</version>
    <classifier>shaping-harfbuzz-plugin</classifier>
</dependency>

and modify the code as shown below:

// Open a document
Document doc = new Document("in.docx");

// When text shaper factory is set, layout starts to use OpenType features.
// An Instance property returns static BasicTextShaperCache object wrapping HarfBuzzTextShaperFactory
doc.getLayoutOptions().setTextShaperFactory(com.aspose.words.shaping.harfbuzz.HarfBuzzTextShaperFactory.getInstance());

// Get page count.
System.out.println(doc.getPageCount());

See more about advanced typograph features in the documentation:
https://docs.aspose.com/words/java/enable-opentype-features/

Thanks,
I got the correct page count by using this code.

Is there another way to get correct page count without changing codes?
Like just install truetype font on server according to
https://docs.aspose.com/words/java/install-truetype-fonts-on-linux/

@GusGus I am afraid, no, there is no way to get correct page count for this document without enabling open type features.

Would there be any bad effects to apply doc.getLayoutOptions().setTextShaperFactory(com.aspose.words.shaping.harfbuzz.HarfBuzzTextShaperFactory.getInstance()); to all documents, even when it’s not necessary?

@GusGus There should not be any problems with documents that do not required open type features.

I have one more question, how can I get one section’s page count?

@GusGus You can use LayoutCollector.getNumPagesSpanned method:

Document doc = new Document("C:\\Temp\\in.docx");
LayoutCollector collector = new LayoutCollector(doc);
System.out.println(collector.getNumPagesSpanned(doc.getLastSection()));