The table of contents is a field in a Word document that displays the headings of all levels and their corresponding page numbers in the document. Moreover, the table of contents can be updated after changes are made to the content of the document, and new headings and page numbers can be generated according to the new document content. Therefore, the table of contents can be a great convenience both when editing documents and reading them, while also making them more professional. This article is going to show how to insert the table of contents into Word documents through Java programs using Spire.Doc for Java.
- Create a Table of Contents with the Default Format
- Create a Table of Contents with Customized Format
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.10.3</version> </dependency> </dependencies>
Create a Table of Contents with the Default Format
Before creating a table of contents, it is necessary to set the level for each heading, so that the table of contents can be generated according to each heading and its level. The level of a heading can be set by using the Paragraph.getListFormat().setListLevelNumber(int) method.
Spire.Doc for Java provides the Paragraph.appendTOC() method to insert a table of contents into a paragraph. The detailed steps for inserting a table of contents with the default style in a Word document are as follows:
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Add a section to the document using Document.addSection() method and insert it into the document after the cover section using Document.getSections().insert() method.
- Add a paragraph to the section using Section.addParagraph() method.
- Create a table of contents in the paragraph using Paragraph.appendTOC() method.
- Update the table of contents to display the headings in the document and their page numbers using Document.updateTableOfContents() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.Section; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.TableOfContent; public class insertTableOfContents { public static void main(String[] args) { //Create an object of Document class Document doc = new Document(); //Load a Word document doc.loadFromFile("Sample.docx"); //Add a section and insert it into the document after the cover section Section section = doc.addSection(); doc.getSections().insert(1, section); //Add a paragraph to the section Paragraph paragraph = section.addParagraph(); paragraph.appendText("Table of Contents\r\n"); paragraph.getStyle().getCharacterFormat().setFontSize(14f); //Create a table of contents in the paragraph paragraph.appendTOC(2, 3); //Update the table of contents doc.updateTableOfContents(); //Save the document doc.saveToFile("CreateTableOfContents.docx"); } }
Create a Table of Contents with Customized Format
In Spire.Doc for Java, the TableOfContent class represents a table of contents of a Word document. The parts displayed in the table of contents can be customized by the switches while initializing an instance of the TableOfContent class. For example, the switches, "{\\o \"1-3\" \\n 1-2}", mean displaying the first to third level headings while omitting the page numbers of the first and second level headings.
The detailed steps for customizing a table of contents in a Word document are as follows:
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Insert a section after the cover section using Document.getSections().insert() method and add a paragraph to the section using Section.addParagraph() method.
- Create an instance of TableOfContent class to create a table of contents that displays the first to third level headings and omits page numbers of the first and second level headings.
- Insert the table of contents to the paragraph using Paragraph.getItems().add() method.
- Insert a field separator mark and a field end mark to the paragraph using Paragraph.appendFieldMark() method to end the table of contents field.
- Set the instance as the table of contents of the document using Document.setTOC() method.
- Update the table of contents to display the current contents for the document using Document.updateTableOfContents() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.Section; import com.spire.doc.documents.FieldMarkType; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.TableOfContent; public class insertTableOfContentsWithCustomizedStyle { public static void main(String[] args) { //Create an object of Document class Document doc = new Document(); //Load a Word document doc.loadFromFile("Sample.docx"); //Insert a section after the first section and add a paragraph in the section Section section = doc.addSection(); doc.getSections().insert(1, section); Paragraph paragraph = section.addParagraph(); paragraph.getStyle().getCharacterFormat().setFontSize(14f); //Initialize an instance of TableOfContent class TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\n 1-2}"); //Insert the table of contents to the document paragraph.getItems().add(toc); //Insert field marks to end the field paragraph.appendFieldMark(FieldMarkType.Field_Separator); paragraph.appendFieldMark(FieldMarkType.Field_End); //Set the created table of contents as the table of contents of the document doc.setTOC(toc); //Update the table of contents doc.updateTableOfContents(); //Save the document doc.saveToFile("TableOfContentsWithCustomizedStyle.docx"); doc.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.