The Syncfusion .NET Word Library (DocIO) is a feature-rich and high-performance tool that allows users to create and update the table of contents (TOC) in a Word document using C# without relying on Microsoft Word or interop dependencies. Build the TOC based on headings with custom styles. Include page numbers and hyperlinks for easy navigation.
This example code shows how to update a table of contents in a Word document using the Syncfusion .NET Word Library with just a few lines of code in C#.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
//Create a new Word document.
using WordDocument document = new WordDocument();
//Add the TOC in the first section.
IWSection section = document.AddSection();
IWParagraph paragraph = section.AddParagraph();
string paraText = "AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.";
//Append TOC for heading levels 1 to 3.
paragraph.AppendTOC(1, 3);
//Add the first chapter.
section = document.AddSection();
paragraph = section.AddParagraph();
paragraph.AppendText("First Chapter");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
section.AddParagraph().AppendText(paraText);
//Add the second chapter.
section = document.AddSection();
paragraph = section.AddParagraph();
paragraph.AppendText("Second Chapter");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
section.AddParagraph().AppendText(paraText);
//Add the third chapter.
section = document.AddSection();
paragraph = section.AddParagraph();
paragraph.AppendText("Third Chapter");
paragraph.ApplyStyle(BuiltinStyle.Heading3);
section.AddParagraph().AppendText(paraText);
//Update the table of contents.
document.UpdateTableOfContents();
//Save the Word document.
using FileStream outputFileStream = new FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite);
document.Save(outputFileStream, FormatType.Docx);
Customize the visual appearance of TOC entries, accommodating up to 9 levels of entries.
Define and use custom styles in the TOC, mapping them to specific heading levels instead of relying solely on default heading styles.
Modify the text of TOC entries, change tab leaders, customize heading levels, manage page numbers, add hyperlinks, and more.
Generate a table of captions for figures, tables, charts, and other numbered items using SEQ fields. Additionally, you can create a table of captions without labels and numbers.
Discover resources from our blog and knowledge base to enhance your efficiency in using tables of contents in Word documents.
Knowledge base
Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.
Product Updates
Technical Support
Yes, you can customize the entries of a TOC in a Word document using the .NET Word Library.
Yes, you can insert multiple TOCs in a single Word document using the .NET Word Library.
Yes, similar to Word documents, you can navigate to the corresponding headings from the TOC in the PDF converted from a Word document.
Yes, the .NET Word Library generates TOCs with hyperlinks and page numbers.
Yes, the .NET Word Library supports creating hierarchical TOCs with multiple levels of headings.
You can access it through the Syncfusion.DocIORenderer.Net.Core NuGet package. Detailed code samples and video tutorials are available in the documentation.
Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion® is proud to hold the following industry awards.