Insert a Table of Contents in Word using Java

Insert a table of contents in Word using Java by adhering to the steps defined in this brief article. It guides to setting up the environment by elaborating the necessary resources, a list of defined steps for developing the application, and a working sample code demonstrating how to add contents page in Word using Java. You will be guided to add a TOC to an existing Word file by parsing its contents.

Steps to Create a Table of Contents in Word using Java

  1. Establish the environment to use Aspose.Words for Java to insert TOC
  2. Access the Word file into the Document object and initialize the DocumentBuilder object
  3. Insert the title of the TOC with the desired style
  4. Add a table of contents and insert a page break
  5. Create the empty table of contents
  6. Save the new Word file having TOC inside it

You can insert contents page in Word using Java by following the above mentioned steps, whereby the process is initiated by loading the source Word file and configuring the DocumentBuilder class instance that supports inserting a table of contents. Insert the title and table of contents using the insertTableOfContents() method and populate the empty table of contents by using the updateFields() method.

Code to Create a Table of Contents in Word using Java

import com.aspose.words.BreakType;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.FindReplaceDirection;
import com.aspose.words.FindReplaceOptions;
import com.aspose.words.License;
import com.aspose.words.ParagraphAlignment;
import com.aspose.words.ParagraphFormat;
public class TocInWord {
public static void main(String[] tocArguments) throws Exception {
String path ="/Users/Data/";
// Apply API Java license to add TOC
new License().setLicense(path+"Conholdate.Total.Product.Family.lic");
// Access the source Word document
Document doc = new Document("example03.docx");
// Instantiate the DocumentBuilder class instance
DocumentBuilder builder = new DocumentBuilder(doc);
// Create ParagraphFormat instance from the builder
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
// Get the existing style name
String defaultStyle = paragraphFormat.getStyleName();
// Set the style name and text alignment for the TOC
paragraphFormat.setStyleName("Title");
paragraphFormat.setAlignment(ParagraphAlignment.CENTER);
// Add title of TOC
builder.writeln("Table of contents");
// Set the text style for the TOC
paragraphFormat.setStyleName(defaultStyle);
//Insert a default table of contents
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
//Add a page break for the TOC
builder.insertBreak(BreakType.PAGE_BREAK);
// Generate the default empty table of contents
doc.updateFields();
// Save the Word document having TOC
doc.save("output.docx");
}
}

Using this example, you can access an existing Word document and make a contents page in Word using Java. The insertTableOfContents() method requires switches to control the behavior of the table of contents, e.g. 1-3 is used for addressing Heading styles 1, 2, and 3, ‘\h’ is used for setting the hyperlinks, and ‘\u’ is used for setting the outline levels. The default Table of Contents (TOC) is empty and can be populated using the updateFields() method.

This example has covered how to add a table of contents in Word using Java. To flip the text inside the Word document, refer to the article on flip the Text in Word using Java.

 English