While generating Word documents in Java, you may need to create a table of contents (TOC). You can find various solutions for creating TOC, however, you have to write complex code and you may face layout issues. So let me provide you with a simple and easy-to-implement method to create a table of contents in Word documents in Java.

Create TOC in Word

Table of Contents (TOC) in a Word DOC gives you an overview of what a document contains. Furthermore, it lets you navigate to a particular section of the document. In the following sections, you will learn a step-by-step method to create a table of contents in a Word document. Also, you will see how to extract, update, and remove a table of contents in a Word document in Java.

C# API to Create Table of Contents in Word Documents

To work with tables of contents in Word documents, we will use Aspose.Words for .NET. The said API is designed to perform basic as well as advanced Word automation features from within .NET applications. Furthermore, it gives you a sound grip over the manipulation of table of contents in Word documents. You can either download the API or install it using NuGet.

PM> Install-Package Aspose.Words

How to Create Table of Contents in Word in C#

The following are the steps to create a table of contents in a Word document in C#.

The following code sample shows how to add a table of contents in a Word document in C#.

Extract Table of Contents from a Word Document in C#

The following are the steps to extract fields from a table of contents in a Word document.

  • Load the Word document using the Document class.
  • Loop through each Field in the document using Document.Range.Fields collection.
  • Check if the field type is a hyperlink using Field.Type property.
  • Check if the field comes under the table of contents section.
  • Retrieve the information of the field and print it.

The following code sample shows how to extract a table of contents from a Word document using C#.

C# Update Table of Contents in Word Document

Whenever you append some content to a Word document, you need to update the table of contents. To do it programmatically, you simply need to call Document.UpdateFields() method before saving the document.

Remove Table of Contents in Word Document

Aspose.Words for .NET also allows you to remove the table of contents from a Word document. The following are the steps to perform this operation.

  • First, load the Word document using the Document class.
  • Get and store the FieldStart nodes of each TOC in an array.
  • Loop through the nodes until you get the node of type FieldEnd (end of the TOC).
  • Remove the nodes using Node.Remove() method and save the updated document.

The following code sample shows how to remove the table of contents from a Word document in C#.

Create Table of Contents with a Free License

You can get a free temporary license and create a table of contents in Word documents without evaluation limitations.

Conclusion

In this article, you have learned how to work with a table of contents in a Word document programmatically. The step-by-step guide and code samples have shown how to add, update, extract and remove the table of contents from Word documents in C#.

C# Word Processing API - Explore More

You can explore more about C# Word automation API using documentation. In case you would have any questions or concerns, please contact us via our forum.

See Also