Read the complete article on adding or updating table of contents in Word documents using Java: https://blog.aspose.com/2021/07/02/add-or-update-toc-in-word-using-java/
Last active
July 2, 2021 15:36
-
-
Save aspose-com-gists/aa89da3b6a5f420a2953413006080c54 to your computer and use it in GitHub Desktop.
Add or Update Table of Contents in Word using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the Word document | |
Document doc = new Document("Word.docx"); | |
// Create a document builder | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert a table of contents at the beginning of the document. | |
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u"); | |
builder.insertBreak(BreakType.PAGE_BREAK); | |
// The newly inserted table of contents will be initially empty. | |
// It needs to be populated by updating the fields in the document. | |
doc.updateFields(); | |
// Save the updated document | |
doc.save("Output.docx"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the Word document | |
Document doc = new Document("Word.docx"); | |
// Update table of content | |
doc.updateFields(); | |
// Save the updated document | |
doc.save("Output.docx"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment