Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active July 2, 2021 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/aa89da3b6a5f420a2953413006080c54 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/aa89da3b6a5f420a2953413006080c54 to your computer and use it in GitHub Desktop.
Add or Update Table of Contents in Word using Java
// 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");
// 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