Page Numbers of TOC field are incorrect in output DOCX using Java

When saving a generated document as .doc or .docx the page numbers in the TOC are wrong. Opening the document and updating the index manually fixes it. Saving the exact same generated document as a PDF gives an identical TOC with the same page numbers, but in the PDF the numbers matches the pages. Even though saved from the same generated document the .doc and .docx have fewer pages.
Last bit of code to run before we save is:
doc.updateFields();
doc.updatePageLayout();
Switching the two lines and running them in opposite order does not help or affect the result. Any tips on how to fix this?

@petteraas

To ensure a timely and accurate response, please attach the input, problematic output and expected output documents here for testing. We will investigate the issue and provide you more information on it.

PS: To attach these resources, please zip and upload them.

Please see the attached documents. The .pdf is correct, while the .docx shows the problem. There are no input documents. These documents are generated completely from scratch. The frontpage with the TOC is created first. Then each “subdocument” is generadted seperatly and appended with
doc.appendDocument(subDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

Output_documents.zip (877.7 KB)

@petteraas

Please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Please also share the sub-documents. We will investigate this issue and provide you more information on it.

There are no subdocuments to share. EVERYTHING is generated from scratch. You can see the result in the previously attached documents.
The java-code attached is a very simplified version of how we create them. The attached code will generate a document and save it as both .docx and .pdf. The .docx-file shows a page number offset in the TOC.

AsposePageNumbersOffset.java.zip (2.5 KB)

@petteraas

We have tested the scenario using the latest version of Aspose.Words for Java 21.1 and have not found the shared issue. So, please use Aspose.Words for Java 21.1. We have attached the output DOCX and PDF with this post for your kind reference.
output documents.zip (221.3 KB)

We are observing the same problem using 21.1. Please see the attached code and output where the .docx-document has offset page numbers in the TOC.

AsposePageNumbersOffset.java_2.zip (2.5 KB)

output_documents_2.zip (160.5 KB)

@petteraas

Please add the following code snippet before saving document at your end to get the missing fonts notifications. Please share the notifications if you face any. You need to install the missing fonts on the system where you are creating the documents.

doc.setWarningCallback(new com.aspose.words.IWarningCallback() {
    @Override
    public void warning(com.aspose.words.WarningInfo warningInfo) {
        if(warningInfo.getWarningType() == WarningType.FONT_SUBSTITUTION)
            System.out.println(warningInfo.getDescription());
    }
});

// 4. Save
try {
    doc.save(MyDir + "AsposePageNumbersOffset.docx", SaveFormat.DOCX);
    PdfSaveOptions pso = new PdfSaveOptions();
    pso.setSaveFormat(SaveFormat.PDF);
    doc.save(MyDir + "AsposePageNumbersOffset.pdf", pso);
} catch (Exception ex) {
    System.out.println("Could not save document");
}

Since you mentioned fonts I re-installed my MS-fonts. I get no notifications, but there is STILL an offset. The offset is less with reinstalled fonts, but it it still there.

@petteraas

The issue you are facing is related to missing fonts. Please check the fonts used in PDF in attached image.
fonts.png (4.5 KB)

The correct fonts are ‘Arial’ and ‘Times New Roman’ and these are correctly used in the documents generated at our end. Please make sure that these fonts are installed at your system.

We tested your scenario at Windows 10 operating system and have not found the TOC page number issue. Could you please share your working environment?

As previously stated, the error is still present even with MS-fonts reinstalled.

Local test environment: Linux Mint 19.1
Live environment: Ubuntu 18.04.5 LTS

Please see the attached documents from both a local run of the previously shared test-code, and from our live environment. Both the PDFs have MS-fonts embedded. BOTH the .docx-documents have offsets on their TOC’s.

output_documents_all.zip (1.1 MB)

@petteraas

Thanks for sharing the detail. We have not found any issue with output_test_code.pdf and output_test_code.docx. Please check the attached screenshots.
pdf.png (55.9 KB)
docx.png (35.9 KB)

However, the page numbers of TOC are incorrect in output_live_enviroment.docx. Please share the sample application that generates this document. We will investigate the issue and provide you more information on it.

There is no sample application that CAN genereate that document. It is from the LIVE application and consists of data from multiple sources (both database and xml) and the code is thousands of lines.
Please see the attached images of output_test_code.docx. In the TOC it says that document 22 starts on page 105, but in reality it starts on page 106.

images.zip (392.2 KB)

@petteraas

Unfortunately, it is difficult to say what the problem is without code example. The code example that you shared in this thread generates correct output. We need sample application to reproduce this issue at our end. Thanks for your cooperation.

I have same issue , i think it is cause by font

@Nealc You are right, the problem with page numbering in TOC might be caused by fonts. To calculate TOC page numbers Aspose.Words has to build document layout, the fonts used in the document are required for this process. If some fonts are not available, Aspose.Words substitutes the missed fonts. This might cause difference in the document layout and as a result difference in page numbers in the TOC.

It’s a strange issue, it’s normal after I install MS font in linux

log show many font was replace by other font and its work

In Windows 11 has all font , there has wrong page number in the TOC.
index name isTrueType
===4==黑体===true===
===5==隶书===true===
===6==楷体_GB2312===false===
===7==Cambria===true===
===8==Courier New===true===
===9==Calibri===true===
===10==华文中宋===true===
===11==Segoe UI Emoji===true===

Use MS Office save doc with embeded font and without update TOC as a template to generate a doc, TOC page number is correct.

Document doc = new Document("1.docx");
doc.updateFields();
doc.save("2.docx");

I try set LoadOptions and FontSetting and it’s not working

@Nealc Could you please attach your input and output documents here for testing? We will check the issue and provide you more information.
Also, it looks like you are using old version of Aspose.Words for Java. In the latest version you should use both updateFields and updatePageLayout methods to update TOC in the document. If so, please try using the latest 22.7 version of Aspose.Words for Java.