Table of Contents (TOC) is an important part of a Word Document. It provides an overview of the document’s content and allows you to navigate to your desired section quickly. You might find yourself in scenarios where you need to add, extract, update or remove the table of contents from Word documents programmatically. To that end, this article will teach you how to work with the table of contents in Word files using C++.
- C++ API for Working with Table of Contents in Word Documents
- Add the Table of Contents in a Word Document
- Extract the Table of Contents from a Word Document
- Update the Table of Contents in a Word Document
- Remove the Table of Contents from a Word Document
C++ API for Working with Table of Contents in Word Documents
Aspose.Words for C++ is a native C++ library that allows you to create, read, modify and convert Microsoft Word documents. In addition, it also supports working with the table of contents in Word files. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Words.Cpp
Add the Table of Contents in a Word Document
The following are the steps to add a table of contents in a Word document.
- Load the Word file using the Document class.
- Create an instance of the DocumentBuilder class using the Document object created previously.
- Insert the table of contents using the DocumentBuilder->InsertTableOfContents(System::String switches) method.
- Populate the table of contents using the Document->UpdateFields() method.
- Save the Word document using the Document->Save(System::String fileName) method.
The following sample code shows how to add a table of contents in a Word document using C++.
Extract the Table of Contents from a Word Document
The following are the steps to extract the table of contents from a Word document.
- Load the Word file using the Document class.
- Retrieve the fields using Document->get_Range()->get_Fields() method and loop over them.
- Check whether the field is of the type FieldType::FieldHyperlink.
- Check if the field belongs to the table of contents.
- Retrieve and print the field information.
The following sample code demonstrates how to extract the table of contents from a Word document using C++.
Update the Table of Contents in a Word Document
If the document’s content has been updated, and you need to reflect those changes in the table of contents, you simply need to load the Word file and call the Document->UpdateFields() method. This method will update the table of contents according to the modified content. After this, save the updated Word document.
Remove the Table of Contents from a Word Document
The following are the steps to remove the table of contents from a Word document.
- Load the Word file using the Document class.
- Retrieve and store a list of the FieldStart nodes.
- Loop through the nodes until you reach the node of the type NodeType::FieldEnd that designates the end of the table of contents.
- Remove the table of contents using the Node->Remove() method.
- Save the Word document using the Document->Save(System::String fileName) method.
The following sample code shows how to remove the table of contents from a Word Document using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to work with the table of contents in Word documents using C++. You have seen the steps and the sample code required to add, extract, update and remove the table of contents from Word documents. Aspose.Words for C++ provides many additional features for working with Word files. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.