Set Locale Culture & Update TOC Field in DOCX Word Document using Java & avoid "no table of contents entries found" Message

Updating the fields on a TOC that was loaded from a docx file results in “NO TABLE OF CONTENTS ENTRIES FOUND”.

The source code is:

Document docWord = new Document("ERROR.docx");
docWord.updateFields();
docWord.updatePageLayout(); //If we remove this line the error persists
docWord.save("ERROR_2.docx");

I attach the docx file converted to PDF (the forum doesn’t allow to upload docx filesERROR.pdf (12.2 KB)
).

Hi Lluis,

Thanks for your inquiry. We need to investigate your source document, you may please share your input and output Word documents as ZIP file. We will look into the issue and will guide you accordingly.

Best Regards,

Hi Tilal, I attach the Word document in a ZIP.

Best regards

ERROR.zip (20.8 KB)

Hi Lluis,

Thanks for sharing source document. Please note the custom TOC of your DOCX file are created under some different language culture instead of English culture. The TOC field is separated by “;” instead of “,”. To update TOC under English culture you need to either replace “;” with “,” in TOC field or change your application Locale accordingly. I have tried the scenario with French culture and it is working as expected.

Locale locale = new Locale("fr","FR");
Locale.setDefault(locale);
com.aspose.words.Document docWord = new com.aspose.words.Document("ERROR.docx");
docWord.updateFields();
//docWord.UpdatePageLayout(); //If we remove this line the error persists
docWord.save("ERROR_2.docx");

Best Regards,

Hi Tilal, thank you very much for your solution. The TOC was created under spanish culture and it solved my problem.

Currently the used code is the following:

Locale docLocale = new Locale("es","ES");
Locale.setDefault(docLocale);

Document docWord = new Document("Pre-Error.docx");
docWord.updateFields();
docWord.save("Post-Error.docx");

However, I have other document with multiple lists that are not successfully updated with this code.

I attach a zip file with the Word file before being updated (Pre-Error.docx) and the file updated (Post-Error.docx).

Could you help me please?

Best regards

ASPOSE.zip (39.7 KB)

@CinfaSA

Thanks for your feedback. It is good to know that suggested solution worked for you.

However, in reference to your new file Pre-Error.docx, it seems it is expected behavior. If you open your file in MS Word and update the TOC, it is removing the TOC contents. Aspose.Word mimics the behavior of MS Word. Please check and confirm.

Best Regards,

Hi Tilal, you are right, that is the MS behavior, so the problem is finally solved.

Thank you very much for your help.

Best regards