Create Table of Contents in Word in Java

This article demonstrates how to create table of contents in a Word document using Spire.Doc for Java.

The following example shows how to create a table of contents with default appearance that includes all text formatted with built-in styles Heading 1, Heading 2, Heading 3, and page numbers right-aligned with tab leaders.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

import java.awt.*;

public class TableofContents {
    public static void main(String[] args){
        //instantiate a Document object
        Document doc = new Document();
        //add a section
        Section section = doc.addSection();

        //add a paragraph
        Paragraph para = section.addParagraph();
        TextRange tr = para.appendText("Table of Contents");
        //set font size and text color
        tr.getCharacterFormat().setFontSize(11);
        tr.getCharacterFormat().setTextColor(Color.blue);       
        //set the space after the paragraph
        para.getFormat().setAfterSpacing(10);

        //add a paragraph
        para = section.addParagraph();
        //add a table of contents with default appearance by specifying lower heading level and upper heading level. The heading level range must be from 1 to 9.
        para.appendTOC(1, 3);

        //add a new section
        section = doc.addSection();
        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 1");
        //apply Heading 1 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_1);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 2");
        //apply Heading 2 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_2);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 3");
        //apply Heading 3 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_3);
        section.addParagraph();

        //update Table of Contents
        doc.updateTableOfContents();

        //save the resultant document
        doc.saveToFile("createTableOfContents.docx", FileFormat.Docx);
    }
}

Output:

Create Table of Contents in Word in Java

We can also create a custom table of contents and determine what entries to appear in the table of contents by using TOC switches. The following example shows how to create a custom table of contents that includes all text formatted with built-in styles Heading 1, Heading 2 and Heading 3 but omits page numbers from heading levels 1-3.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

import java.awt.*;

public class TableofContents {
    public static void main(String[] args){
        //instantiate a Document object
        Document doc = new Document();
        //add a section
        Section section = doc.addSection();

        //add a paragraph
        Paragraph para = section.addParagraph();
        TextRange tr = para.appendText("Table of Contents");
        //set font size and text color
        tr.getCharacterFormat().setFontSize(11);
        tr.getCharacterFormat().setTextColor(Color.blue);
        //set the space after the paragraph
        para.getFormat().setAfterSpacing(10);

        //create a custom table of contents that omits page numbers from heading levels 1-3.
        TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\n 1-3}");
        para = section.addParagraph();
        para.getItems().add(toc);
        para.appendFieldMark(FieldMarkType.Field_Separator);
        para.appendText("TOC");
        para.appendFieldMark(FieldMarkType.Field_End);
        doc.setTOC(toc);

        //add a new section
        section = doc.addSection();
        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 1");
        //apply Heading 1 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_1);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 2");
        //apply Heading 2 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_2);
        section.addParagraph();

        //add a paragraph
        para = section.addParagraph();
        para.appendText("Heading 3");
        //apply Heading 3 style to the paragraph
        para.applyStyle(BuiltinStyle.Heading_3);
        section.addParagraph();

        //update Table of Contents
        doc.updateTableOfContents();

        //save the resultant document
        doc.saveToFile("customTableOfContents.docx", FileFormat.Docx);

    }
}

Output:

Create Table of Contents in Word in Java