.NET Word Examples
- Create Word Document
- Perform Mail Merge
- Manage Bookmarks
- Find and Replace Text
- Format Tables
- Split Word Documents
- Merge Multiple Word Documents
- Fill and Manage Form Fields
- Generate and Update Table of Contents
- Compare Word Documents
- Encrypt and Decrypt Documents with Password
- Convert Word Documents to PDF
- Convert Word Documents to Images
- Word-HTML Conversions
- Word-Markdown Conversions
- Word-Text Conversions
Add a Table of Contents to a Word Document Using the Syncfusion .NET Word Library
The Syncfusion® .NET Word Library 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.
Watch this video to learn how to create and update the table of contents in a Word document using the Syncfusion .NET Word Library.
Create and update TOC in Word document using C#
Learn how to create and update table of contents (TOC) in Word documents programmatically using C# with the Syncfusion .NET Word Library. This guide demonstrates building TOC based on headings with page numbers and hyperlinks for easy navigation.
Step 1: Create a new project
Start by creating a new C# Console Application project.
Step 2: Install the NuGet package
Add the Syncfusion.DocIO.Net.Core package to your project from NuGet.org.
Step 3: Add required namespaces to create and update TOC
Add the following namespaces to your Program.cs file:
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using System.IO;Step 4: Create a Word document and add section
Create a new Word document and add a section with initial text content.
WordDocument document = new WordDocument();
//Add the section into the Word document
IWSection section = document.AddSection();
string paraText = "AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.";Step 5: Insert table of contents
Insert a table of contents field with specified heading levels to the document.
//Add the paragraph into the created section
IWParagraph paragraph = section.AddParagraph();
//Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determines the TOC entries
paragraph.AppendTOC(1, 3);Step 6: Add first chapter with heading
Add a new section with the first chapter heading and content.
//Add the section into the Word document.
section = document.AddSection();
//Add the paragraph into the created section
paragraph = section.AddParagraph();
//Add the text for the headings
paragraph.AppendText("First Chapter");
//Set a built-in heading style
paragraph.ApplyStyle(BuiltinStyle.Heading1);
//Add the text into the paragraph
section.AddParagraph().AppendText(paraText);Step 7: Add second chapter with heading
Add a new section with the second chapter heading and content.
//Add the section into the Word document
section = document.AddSection();
//Add the paragraph into the created section
paragraph = section.AddParagraph();
//Add the text for the headings
paragraph.AppendText("Second Chapter");
//Set a built-in heading style
paragraph.ApplyStyle(BuiltinStyle.Heading2);
//Add the text into the paragraph
section.AddParagraph().AppendText(paraText);Step 8: Add third chapter with heading
Add a new section with the third chapter heading and content.
//Add the section into the Word document
section = document.AddSection();
//Add the paragraph into the created section
paragraph = section.AddParagraph();
//Add the text into the headings
paragraph.AppendText("Third Chapter");
//Set a built-in heading style
paragraph.ApplyStyle(BuiltinStyle.Heading3);
//Add the text into the paragraph
section.AddParagraph().AppendText(paraText);Step 9: Update TOC
Update the table of contents with the chapter entries.
//Update the table of contents
document.UpdateTableOfContents();Step 10: Save the Word document
Save the Word document to a file stream in DOCX format.
//Create file stream
FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite);
//Save the Word document to file stream
document.Save(outputFileStream, FormatType.Docx);
// Close resource
outputFileStream.Close();
document.Close();GitHub project
NuGet installation
Get started quickly by downloading the installer and checking license information on the Downloads page.
Table of contents
Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.
Learning
Technical Support
