Is there a way to adjust document by switch the order of table of content

For example, the TOC:
1
1.1
1.1.1
1.1.2
1.2
1.2.1
1.2.2

I’d like to move 1.2.1 to the end of 1.1.1
so it would be like
1
1.1
1.1.1
1.1.2
1.1.3
1.2
1.2.1

@GusGus

To adjust the order of entries in a Table of Contents (TOC) using Aspose.Words, you can manipulate the headings in your document. The TOC is generated based on the heading styles applied to the content, so to achieve your desired order, you will need to rearrange the headings accordingly.

Here’s a step-by-step approach to move the entry “1.2.1” to the end of “1.1.1”:

  1. Identify the Headings: Ensure that the headings in your document are correctly styled using the built-in heading styles (e.g., Heading 1, Heading 2, etc.).

  2. Rearrange the Headings: You can programmatically change the order of the headings in the document. For example, you can move the content of “1.2.1” to follow “1.1.1”.

  3. Update the TOC: After rearranging the headings, you will need to update the TOC to reflect these changes. This can be done using the Document.UpdateFields() method followed by Document.UpdatePageLayout() to ensure that the page numbers are correctly displayed.

Here is a sample code snippet demonstrating how to rearrange headings and update the TOC:

from aspose.words import Document, DocumentBuilder

# Load the document
doc = Document("your_document.docx")
builder = DocumentBuilder(doc)

# Assuming you have identified the nodes for the headings
# Move the content of "1.2.1" to follow "1.1.1"
# This is a simplified example; you will need to find the actual nodes in your document
heading_1_1_1 = doc.get_child(NodeType.PARAGRAPH, index_of_1_1_1, True)
heading_1_2_1 = doc.get_child(NodeType.PARAGRAPH, index_of_1_2_1, True)

# Insert heading_1_2_1 after heading_1_1_1
heading_1_1_1.parent_node.insert_after(heading_1_2_1, heading_1_1_1)

# Update the TOC
doc.update_fields()
doc.update_page_layout()

# Save the document
doc.save("updated_document.docx")

Make sure to replace index_of_1_1_1 and index_of_1_2_1 with the actual indices of the headings in your document. This code will rearrange the headings and update the TOC accordingly.

By following these steps, you should be able to customize the order of entries in your Table of Contents as needed.

For more detailed information on working with TOCs in Aspose.Words, you can refer to the official documentation on Working with Table of Contents.

Sources:
[1]: Working with Table of Contents|Aspose.Words for Python via .NET

Do the sub chapters move together, just like office:

The office cloud do this so elegant. :grinning:

@GusGus I am afraid there is no easy way to reorder heading in the TOC. As it was mentioned TOC is generated automatically upon updating fields and items appears in the same order as they appear in the document content. To achieve what you need you should move content in your document to new location. I am afraid there is no general solution for this, all depends on your document structure. Please see our documentation to lean more about Aspose.Words document object model:
https://docs.aspose.com/words/java/aspose-words-document-object-model/

If possible, could you please attach your sample input and expected output documents here for our reference? We will check your documents and provide you more information.