Create and Update Table of Contents (TOC) in Word in Java
Tags: java, word, tableofcontents, toc
Published: 2019-08-26 04:08:50

---
title: Create and Update Table of Contents (TOC) in Word in Java
published: true
description: This article demonstrates how to create and update Table of Contents (TOC) in a Word document in Java.
tags: java, word, table of contents, TOC
---
A table of contents (TOC) provides a quick reference point for your document and gives the readers a brief overview of where to find what content. 

In this article, we’re going to show you how to create and update Table of Contents (TOC) in a Word document within Java application using [Free Spire.Doc for Java](https://www.e-iceblue.com/Introduce/free-doc-for-java.html) library.

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.
```java
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);
    }
}
```
![Create Table of Contents](https://thepracticaldev.s3.amazonaws.com/i/fhxycap8biiem988t8bu.png)
 
We can also create a custom table of contents and determine what entries to appear in the table of contents by using TOC switches. 

| Serial No. | Switches | Description |
| ------------- |:-------------:| ------------:|
| 1 | \o | Builds a table of contents from paragraphs formatted with styles that include outline levels (most commonly, heading styles). |
| 2 | \t | Builds a table of contents from paragraphs formatted with styles other than the built-in styles. |
| 3 | \u | Builds a table of contents from paragraphs whose formatting includes outline levels applied directly, in paragraph settings. |
| 4 | \c | Lists figures, tables, charts, or other items that are numbered by a SEQ (Sequence) field. |
| 5 | \a | Lists items captioned with the Caption command (References > Insert Caption) but omits caption labels and numbers. |
| 6 | \f | Builds a table from TC fields. |
| 7 | \l | Builds a table of contents from TC fields that assign entries to one of the specified levels. |
| 8 | \b | Collects entries only from the portion of the document marked by the specified bookmark. |
| 9 | \s | Includes a number such as a chapter number before the page number. |
| 10 | \d | When used with the \s switch, specifies the character that separates the sequence numbers and page numbers. |
| 11 | \p | Specifies the character that separates an entry and its page number.|
| 12 | \n | Omits page numbers from the table of contents. |
| 13 | \w | Preserves tab entries within table entries. |
| 14 |\x |Preserves manual line breaks within table entries. |
| 15 | \z |Hides tab leader and page numbers in Web layout view. |
| 16 | \h |Inserts TOC entries as hyperlinks. |

For more information regarding TOC switches, check [here](https://support.office.com/en-us/article/field-codes-toc-table-of-contents-field-1f538bc4-60e6-4854-9f64-67754d78d05c?ocmsassetID=HP010255529&CTT=5&origin=HA102110133&CorrelationId=3d50c19b-ff43-4773-a0cd-374096aaa38b&ui=en-US&rs=en-US&ad=US).

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.
```java
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);

    }
}
```
![Custom Table of Contents](https://thepracticaldev.s3.amazonaws.com/i/zm9ktur08b5qzh4pepzs.png)