Aspose.Words for Java filling multiple custom TOCs with the same content

I have a Word document with 2 custom TOCs, each with independent content items. When I run the MS Word option to “Update Table” the results show as correct. When I run the docx into Aspose.Word for Java the result is both TOCs contain the elements meant only for the first one. Since the result from Word shows the correct result, I have to assume the TOC Fields are set up correctly. Is there a special form of the API to generate the TOCs in Aspose that I need to be aware of?

@roydonnelly

To generate multiple custom TOCs (Tables of Contents) in a Word document using Aspose.Words for Java, you need to ensure that each TOC’s range is set correctly. Aspose.Words treats each TOC independently and updates its range based on the bookmarks defined.

Here’s a step-by-step guide and sample code to ensure that your TOCs are updated properly:

Steps

  1. Ensure Correct Bookmark Setup in Word Document:

    • Verify that each TOC in your Word document has a unique bookmark defining its range.
  2. Load Document and Update TOCs using Aspose.Words for Java:

    • Use the updateFields() method to update the TOC fields in the document after loading it.

Sample Code

import com.aspose.words.*;

public class UpdateMultipleTOCs {
    public static void main(String[] args) throws Exception {
        // Load the document
        Document doc = new Document("path/to/your/document.docx");

        // Update all fields in the document
        doc.updateFields();

        // Save the updated document back to the file system
        doc.save("path/to/your/updatedDocument.docx");
    }
}

Key Points

  • Update Fields: By calling updateFields(), Aspose.Words ensures that all fields, including TOC fields, are updated based on the current document state.
  • Bookmarks: Ensure that each TOC has a distinct bookmark range. You can define bookmarks in MS Word:
    1. Open the TOC dialog (References → Table of Contents).
    2. Check the “Use heading styles from” option and ensure the range of your heading styles is correctly defined.

Documentation

For further reference, you can check the Aspose.Words for Java documentation on Field Updating and Bookmark Overview to understand how to work with fields, Bookmarks, and updating them in your documents.

If you encounter specific issues such as incorrect TOC generation or if the code needs modifications based on the structure of your document, please share more details or a sample document for in-depth troubleshooting.

@roydonnelly Could you please attach the problematic input and output documents here for testing? We will check the issue and provide you more information.

I can but I don’t see an Attach method on these message boxes.

@roydonnelly You can just drag and drop your files. If extension of the file is not supported just zip the file and upload ZIP archive.

Thank you. Here are the files zipped. The docx produces the correct output when updated via MS Word. The PDF is the document produced from that docx using Aspose.Word for Java. The second TOC (PDF page 114) should only have 3 entries but shows the same entries as the first TOC (PDF page 11). The styles are clearly distinguished in the field codes.

TOCIssue.zip (2.8 MB)

@roydonnelly Thank you for additional information. Unfortunately, I cannot reproduce the problem on my side using the latest 25.7 version of Aspose.Words and the following simple code:

Document doc = new Document(@"C:\Temp\in.docx");
doc.UpdateFields();
doc.Save(@"C:\Temp\out.pdf");

out.pdf (480.5 KB)

Is this produced using Aspose.Words for Java or for .NET?

@roydonnelly The above provided output was produced by .NET version of Aspose.Words. Here is the output produced by Java version and the following code:

Document doc = new Document("C:\\Temp\\in.docx");
doc.updateFields();
doc.save("C:\\Temp\\out_java.pdf");

out_java.pdf (480.5 KB)

Thanks you very much for your assistance. It may be a version issue on our side.

@roydonnelly I used the latest 25.6 version of Aspose.Words for Java in my tests.