Table of Contents (TOC) in a Word document gives you an overview of what a document contains. Furthermore, it lets you navigate to a particular section of the document. In this article, you will learn how to work with a table of contents in Word documents programmatically. Particularly, this article covers how to add, extract, update or remove a table of contents in Word documents in C#.

C# API to Add, Edit, or Delete TOC in Word Documents

In order 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

Add Table of Contents in a Word Document in C#

The following are the steps to add a table of contents to Word document using Aspose.Words for .NET.

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

C# Extract Table of Contents from a Word Document

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 the 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#.

Update Table of Contents in Word Document in C#

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

Remove Table of Contents in Word Document in C#

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#.

Get a Free API License

In case you want to try the API without evaluation limitations, you can get a free temporary license.

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#. 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