Page Numbers of Table of Contents are Incorrect When Document is Saved to DOCX using Java

Hi there,

When I create a Word file using DocumentBulder, The table of contents seems to be generated with the wrong page numbers. I have to update the Word file manually to get TOC numbering corrected.
It seems to be similar to the issue WORDSNET-832 which has been open for a long time.

I created a test case for your investigation and attach it to this thread.

toc-numbering.zip (75.6 KB)

Regards,
Behrouz

@behrouz

We have tested the scenario using the latest version of Aspose.Words for Java 21.4 and have not found the shared issue. So, please use Aspose.Words for Java 21.4.

@tahir.manzoor

I can still see the issue with the latest version of Aspose. I attached the test case with the new version plus a document generated by the test case.

output.docx.zip (30.0 KB)
toc-numbering.zip (81.4 KB)

@behrouz

We have tested the scenario at Windows 10 using Java 8. We have not found the shared issue. Please check the attached output document. 21.4 out.zip (29.5 KB)

Could you please share the your working environment e.g. operating system, Java version etc.? We will investigate the issue at same environment and provide you more information on your issue.

@tahir.manzoor

I reproduced the issue on macOS using java 8 and Microsoft Word v16.47.

@behrouz Note that to update page numbers in the TOC Aspose.Words performs document layout. To perform document layout operation Aspose.Words requires fonts used in the document. If the fonts are not available it uses alternative fonts and in this case layout of the document might be different and as a result page numbers might be different too.
So to get accurate result, please, make sure the required fonts are available in your environment. See the following article for more information.

@alexey.noskov

Is there any way to see what fons are missing in the generated document by Aspose?

@behrouz

Please implement IWarningCallback interface as shown below to get the missing fonts notification.

Document doc = new Document(MyDir + "input.docx"); 
doc.setWarningCallback(new IWarningCallback() {
    @Override
    public void warning(WarningInfo warningInfo) {
        if (WarningType.FONT_SUBSTITUTION == warningInfo.getWarningType()) {
            System.out.println(warningInfo.getDescription());
        }
    }
});

doc.save(MyDir + "21.4.pdf"); 

Moreover, we suggest you please read the following article.