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.

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 Add, Edit, or Delete 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 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#.
- 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#.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); | |
// Initialize document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert a table of contents at the beginning of the document. | |
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u"); | |
// The newly inserted table of contents will be initially empty. | |
// It needs to be populated by updating the fields in the document. | |
doc.UpdateFields(); | |
dataDir = dataDir + "DocumentBuilderInsertTOC_out.doc"; | |
doc.Save(dataDir); |
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#.
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); | |
string fileName = "TOC.doc"; | |
Aspose.Words.Document doc = new Aspose.Words.Document(dataDir + fileName); | |
foreach (Field field in doc.Range.Fields) | |
{ | |
if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink)) | |
{ | |
FieldHyperlink hyperlink = (FieldHyperlink)field; | |
if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc")) | |
{ | |
Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph); | |
Console.WriteLine(tocItem.ToString(SaveFormat.Text).Trim()); | |
Console.WriteLine("------------------"); | |
if (tocItem != null) | |
{ | |
Bookmark bm = doc.Range.Bookmarks[hyperlink.SubAddress]; | |
// Get the location this TOC Item is pointing to | |
Paragraph pointer = (Paragraph)bm.BookmarkStart.GetAncestor(NodeType.Paragraph); | |
Console.WriteLine(pointer.ToString(SaveFormat.Text)); | |
} | |
} // End If | |
}// End If | |
}// End Foreach |
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#.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public static void Run() | |
{ | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithStyles(); | |
// Open a document which contains a TOC. | |
Document doc = new Document(dataDir + "Document.TableOfContents.doc"); | |
// Remove the first table of contents from the document. | |
RemoveTableOfContents(doc, 0); | |
dataDir = dataDir + "Document.TableOfContentsRemoveToc_out.doc"; | |
// Save the output. | |
doc.Save(dataDir); | |
Console.WriteLine("\nSpecified TOC from a document removed successfully.\nFile saved at " + dataDir); | |
} | |
/// <summary> | |
/// Removes the specified table of contents field from the document. | |
/// </summary> | |
/// <param name="doc">The document to remove the field from.</param> | |
/// <param name="index">The zero-based index of the TOC to remove.</param> | |
public static void RemoveTableOfContents(Document doc, int index) | |
{ | |
// Store the FieldStart nodes of TOC fields in the document for quick access. | |
ArrayList fieldStarts = new ArrayList(); | |
// This is a list to store the nodes found inside the specified TOC. They will be removed | |
// At the end of this method. | |
ArrayList nodeList = new ArrayList(); | |
foreach (FieldStart start in doc.GetChildNodes(NodeType.FieldStart, true)) | |
{ | |
if (start.FieldType == FieldType.FieldTOC) | |
{ | |
// Add all FieldStarts which are of type FieldTOC. | |
fieldStarts.Add(start); | |
} | |
} | |
// Ensure the TOC specified by the passed index exists. | |
if (index > fieldStarts.Count - 1) | |
throw new ArgumentOutOfRangeException("TOC index is out of range"); | |
bool isRemoving = true; | |
// Get the FieldStart of the specified TOC. | |
Node currentNode = (Node)fieldStarts[index]; | |
while (isRemoving) | |
{ | |
// It is safer to store these nodes and delete them all at once later. | |
nodeList.Add(currentNode); | |
currentNode = currentNode.NextPreOrder(doc); | |
// Once we encounter a FieldEnd node of type FieldTOC then we know we are at the end | |
// Of the current TOC and we can stop here. | |
if (currentNode.NodeType == NodeType.FieldEnd) | |
{ | |
FieldEnd fieldEnd = (FieldEnd)currentNode; | |
if (fieldEnd.FieldType == FieldType.FieldTOC) | |
isRemoving = false; | |
} | |
} | |
// Remove all nodes found in the specified TOC. | |
foreach (Node node in nodeList) | |
{ | |
node.Remove(); | |
} | |
} |
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.