Update toc didn't work

doc.updateFields(), you have to manually update the entire directory after opening word. Moreover, the table of contents is inconsistent with the first-level heading, and there should be two spaces between the serial number and the title

@Crane

It seems you are experiencing issues with updating the Table of Contents (TOC) in your document using Aspose.Words. Here are some steps and considerations that might help you resolve the problem:

  1. Updating Fields: After inserting a TOC, you need to ensure that you call the updateFields() method on the document to populate the TOC with the headings. This method should be called after you have added all the headings to the document.

  2. Inconsistent TOC Entries: If the TOC is inconsistent with the first-level headings, make sure that the headings in your document are correctly styled. For example, you should use the appropriate heading styles (e.g., Heading 1, Heading 2) when inserting your headings. This ensures that they are recognized by the TOC.

  3. Spacing Between Serial Number and Title: If you need to adjust the spacing between the serial number and the title in the TOC, you may need to customize the TOC format. This can typically be done by modifying the TOC field code or adjusting the styles used for the TOC entries.

Here is a basic example of how to insert a TOC and update it:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a table of contents at the beginning of the document.
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");

// Add headings to the document.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.writeln("Heading 1");

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
builder.writeln("Heading 1.1");
builder.writeln("Heading 1.2");

// Update the TOC to reflect the headings.
doc.updateFields();
doc.save("YourDocumentWithTOC.doc");

Make sure to adjust the styles and formatting as needed to meet your specific requirements. If you continue to face issues, consider checking the Aspose documentation or community forums for more detailed guidance on customizing TOC entries.

If you have any further questions or need additional assistance, feel free to ask!

I updated it before I saved it, but it didn’t work

@Crane Could you please attach the problematic input and output documents here for testing? We will check the issue and provide you more information. Unfortunately, it is impossible to analyze the issue by screenshot without real documents.

When I use %0.%1 the listTemplate directory doesn’t update, using \u0000.\u0001does.

@Crane Number format is specified in \u0000.\u0001 form. This is expected.
https://reference.aspose.com/words/java/com.aspose.words/listlevel/#getNumberFormat

1 Like

I want to adjust the number of spaces between the number and the text in ‘toc’, what should I do.


I’d like to see two spaces like in the list

@Crane Could you please attach your input and expected output documents here for our reference? We will check your documents and provide you more information.

  @Test
  void test6() throws Exception {
    Document doc = new Document();
    com.aspose.words.List list = doc.getLists().add(ListTemplate.NUMBER_DEFAULT);
    ListLevel listLevel0 = list.getListLevels().get(0);
    listLevel0.setNumberFormat("第\u0000章");
    listLevel0.setNumberStyle(NumberStyle.ARABIC);
    listLevel0.setTrailingCharacter(ListTrailingCharacter.NOTHING);

    ListLevel listLevel1 = list.getListLevels().get(1);
    listLevel1.setNumberFormat("\u0000.\u0001");
    listLevel1.setNumberStyle(NumberStyle.ARABIC);
    listLevel1.setTrailingCharacter(ListTrailingCharacter.NOTHING);

    ListLevel listLevel2 = list.getListLevels().get(2);
    listLevel2.setNumberFormat("\u0000.\u0001.\u0002");
    listLevel2.setNumberStyle(NumberStyle.ARABIC);
    listLevel2.setTrailingCharacter(ListTrailingCharacter.NOTHING);

    DocumentBuilder documentBuilder = new DocumentBuilder(doc);
    documentBuilder.getCurrentParagraph().getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
    documentBuilder.getCurrentParagraph().getListFormat().setList(list);
    documentBuilder.getCurrentParagraph().getListFormat().setListLevelNumber(0);
    documentBuilder.writeln("  开篇");

    documentBuilder.getCurrentParagraph().getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
    documentBuilder.getCurrentParagraph().getListFormat().setList(list);
    documentBuilder.getCurrentParagraph().getListFormat().setListLevelNumber(1);
    documentBuilder.writeln("  一级节");

    documentBuilder.getCurrentParagraph().getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3);
    documentBuilder.getCurrentParagraph().getListFormat().setList(list);
    documentBuilder.getCurrentParagraph().getListFormat().setListLevelNumber(2);
    documentBuilder.writeln("  二级节");

    documentBuilder.insertTableOfContents("TOC \\o \"1-3\" \\h \\z \\u");
    doc.updateFields();
    doc.save("toc_space_res.docx");
  }

expect:
hope_toc_space_res.docx (11.8 KB)

MS word auto-generated table of contents doesn’t work like this either. :rofl:

@Crane There is no way to have different trailing character in list item and in the TOC created from this list item. You cannot also achieve this in MS Word, after updating TOC changes made manually to the TOC will be lost.