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 using C#. Particularly, this article covers how to add, extract, update or remove a table of contents in Word documents.
- C# API to Work with TOC in Word Documents
- Add Table of Contents in a Word Document
- Extract Table of Contents from a Word Document
- Update Table of Contents in a Word Document
- Remove Table of Contents in a Word Document
- Get a Free API License
C# API to Work with TOC in Word Document
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 using C#
The following are the steps to add a table of contents to Word document using Aspose.Words for .NET.
- Create an instance of the Document class (in case of loading an existing Word document, provide the file’s path in the constructor).
- Create an instance of DocumentBuilder class and initialize it with the Document object created earlier.
- Insert table of contents using DocumentBuilder.InsertTableOfContents(“\\o \”1-3\” \\h \\z \\u”) method.
- Update the fields using Document.UpdateFields() method.
- Save the Word document using Document.Save(String) method.
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 using 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 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 using 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 using 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 using C#. The step-by-step guide and code samples have shown how to add, update, extract and remove the table of contents from Word documents. 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.