Use DocumentBuilder to inserting a Table of Contents within C# and .NET

  1. Add SautinSoft.Document from Nuget.
  2. Create a new document.
  3. Create a Builder based on the document.
  4. Use the method InsertTableOfContents.

   The TOC (Table of Contents) field builds a table of contents. The TOC field collects entries for a table of contents using heading styles, other specified styles, outline levels, captions, or entries specified by TC (Table of Contents Entry) fields. Use DocumentBuilderInsertTableOfContents method to insert a TOC (table of contents) field into the document at the current position.
To build a table of contents and display it according to page numbers, call the TableOfEntriesUpdate method.

Complete code

using System;
using SautinSoft.Document;
using System.Text;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/
            InsertingToc();
        }
        /// <summary>
        /// Insert a TOC (Table of Contents) field into the document using DocumentBuilder.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-toc.php
        /// </remarks>

        static void InsertingToc()
        {
            DocumentCore dc = new DocumentCore();
            DocumentBuilder db = new DocumentBuilder(dc);

            // Insert the formatted text into the document using DocumentBuilder.
            db.CharacterFormat.FontName = "Verdana";
            db.CharacterFormat.Size = 16;
            db.Writeln("Table of Contents.");
            db.CharacterFormat.ClearFormatting();

            // Insert Table of Contents field into the document at the current position.
            TableOfEntries toe = db.InsertTableOfContents("\\o \"1-3\" \\h");
            // For information about switches, see the description on the page above.

            // Add the text and divide it into headings.
            db.InsertSpecialCharacter(SpecialCharacterType.PageBreak);
            ParagraphStyle Heading1Style = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Heading1, dc);
            dc.Styles.Add(Heading1Style);
            db.ParagraphFormat.Style = Heading1Style;
            db.Writeln("Heading 1");
            db.ParagraphFormat.ClearFormatting();
            db.Writeln("Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1" +
                "Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1 ");

            ParagraphStyle Heading2Style = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Heading2, dc);
            dc.Styles.Add(Heading2Style);
            db.ParagraphFormat.Style = Heading2Style;
            db.Writeln("Heading 1.1");
            db.ParagraphFormat.ClearFormatting();
            db.Writeln("Some text Heading 1.1 Some text Heading 1.1 Some text Heading 1.1 Some text Heading 1.1 Some text Heading 1.1" +
                " Some text Heading 1.1 Some text Heading 1.1 Some text Heading 1.1 Some text Heading 1.1");
            db.ParagraphFormat.Style = Heading2Style;
            db.Writeln("Heading 1.2");
            db.ParagraphFormat.ClearFormatting();
            db.Writeln("Some text Heading 1.2 Some text Heading 1.2 Some text Heading 1.2 Some text Heading 1.2 Some text Heading 1.2" +
                " Some text Heading 1.2 Some text Heading 1.2 Some text Heading 1.2 ");

            ParagraphStyle Heading3Style = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Heading3, dc);
            dc.Styles.Add(Heading3Style);
            db.ParagraphFormat.Style = Heading3Style;
            db.Writeln("Heading 1.1.1");
            db.ParagraphFormat.ClearFormatting();
            db.Writeln("Some text Heading 1.1.1 Some text Heading 1.1.1 Some text Heading 1.1.1 Some text Heading 1.1.1 " +
                " Some text Heading 1.1.1 Some text Heading 1.1.1 Some text Heading 1.1.1 ");
            db.ParagraphFormat.Style = Heading3Style;
            db.Writeln("Heading 1.1.2");
            db.ParagraphFormat.ClearFormatting();
            db.Writeln("Some text 1.1.2 Some text 1.1.2 Some text 1.1.2 Some text 1.1.2");

            db.ParagraphFormat.Style = Heading1Style;
            db.Writeln("Heading 2");
            db.ParagraphFormat.ClearFormatting();
            db.Writeln("Some text Heading 2 Some text Heading 2.");

            db.ParagraphFormat.Style = Heading1Style;
            db.Writeln("Heading 3");
            db.ParagraphFormat.ClearFormatting();
            db.Writeln("Some text Heading 3 Some text Heading 3 Some text Heading 3 Some text Heading 3 Some text Heading 3" +
                 "Some text Heading 3Some text Heading 3Some text Heading 3Some text Heading 3Some text Heading 3");

            db.ParagraphFormat.Style = Heading2Style;
            db.Writeln("Heading 3.1");
            db.ParagraphFormat.ClearFormatting();
            db.Writeln("Some text Heading 3.1 Some text Heading 3.1 Some text Heading 3.1 Some text Heading 3.1 Some text Heading 3.1" +
               "Some text Heading 3.1 Some text Heading 3.1 Some text Heading 3.1");
            
            db.ParagraphFormat.Style = Heading2Style;
            db.Writeln("Heading 3.2");
            db.ParagraphFormat.ClearFormatting();
            db.Writeln("Some text Heading 3.2 Some text Heading 3.2 Some text Heading 3.2 Some text Heading 3.2 Some text Heading 3.2");

            // Update the TOC field (table of contents).
            toe.Update();

            // Save our document into DOCX format.
            string resultPath = @"Result.docx";
            dc.Save(resultPath, new DocxSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports SautinSoft.Document
Imports System.Text

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			InsertingToc()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' Insert a TOC (Table of Contents) field into the document using DocumentBuilder.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-toc.php
		''' </remarks>

		Private Shared Sub InsertingToc()
			Dim dc As New DocumentCore()
			Dim db As New DocumentBuilder(dc)

			' Insert the formatted text into the document using DocumentBuilder.
			db.CharacterFormat.FontName = "Verdana"
			db.CharacterFormat.Size = 16
			db.Writeln("Table of Contents.")
			db.CharacterFormat.ClearFormatting()

			' Insert Table of Contents field into the document at the current position.
			Dim toe As TableOfEntries = db.InsertTableOfContents("\o ""1-3"" \h")
			' For information about switches, see the description on the page above.

			' Add the text and divide it into headings.
			db.InsertSpecialCharacter(SpecialCharacterType.PageBreak)
			Dim Heading1Style As ParagraphStyle = CType(Style.CreateStyle(StyleTemplateType.Heading1, dc), ParagraphStyle)
			dc.Styles.Add(Heading1Style)
			db.ParagraphFormat.Style = Heading1Style
			db.Writeln("Heading 1")
			db.ParagraphFormat.ClearFormatting()
			db.Writeln("Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1" & "Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1 Some text Heading 1 ")

			Dim Heading2Style As ParagraphStyle = CType(Style.CreateStyle(StyleTemplateType.Heading2, dc), ParagraphStyle)
			dc.Styles.Add(Heading2Style)
			db.ParagraphFormat.Style = Heading2Style
			db.Writeln("Heading 1.1")
			db.ParagraphFormat.ClearFormatting()
			db.Writeln("Some text Heading 1.1 Some text Heading 1.1 Some text Heading 1.1 Some text Heading 1.1 Some text Heading 1.1" & " Some text Heading 1.1 Some text Heading 1.1 Some text Heading 1.1 Some text Heading 1.1")
			db.ParagraphFormat.Style = Heading2Style
			db.Writeln("Heading 1.2")
			db.ParagraphFormat.ClearFormatting()
			db.Writeln("Some text Heading 1.2 Some text Heading 1.2 Some text Heading 1.2 Some text Heading 1.2 Some text Heading 1.2" & " Some text Heading 1.2 Some text Heading 1.2 Some text Heading 1.2 ")

			Dim Heading3Style As ParagraphStyle = CType(Style.CreateStyle(StyleTemplateType.Heading3, dc), ParagraphStyle)
			dc.Styles.Add(Heading3Style)
			db.ParagraphFormat.Style = Heading3Style
			db.Writeln("Heading 1.1.1")
			db.ParagraphFormat.ClearFormatting()
			db.Writeln("Some text Heading 1.1.1 Some text Heading 1.1.1 Some text Heading 1.1.1 Some text Heading 1.1.1 " & " Some text Heading 1.1.1 Some text Heading 1.1.1 Some text Heading 1.1.1 ")
			db.ParagraphFormat.Style = Heading3Style
			db.Writeln("Heading 1.1.2")
			db.ParagraphFormat.ClearFormatting()
			db.Writeln("Some text 1.1.2 Some text 1.1.2 Some text 1.1.2 Some text 1.1.2")

			db.ParagraphFormat.Style = Heading1Style
			db.Writeln("Heading 2")
			db.ParagraphFormat.ClearFormatting()
			db.Writeln("Some text Heading 2 Some text Heading 2.")

			db.ParagraphFormat.Style = Heading1Style
			db.Writeln("Heading 3")
			db.ParagraphFormat.ClearFormatting()
			db.Writeln("Some text Heading 3 Some text Heading 3 Some text Heading 3 Some text Heading 3 Some text Heading 3" & "Some text Heading 3Some text Heading 3Some text Heading 3Some text Heading 3Some text Heading 3")

			db.ParagraphFormat.Style = Heading2Style
			db.Writeln("Heading 3.1")
			db.ParagraphFormat.ClearFormatting()
			db.Writeln("Some text Heading 3.1 Some text Heading 3.1 Some text Heading 3.1 Some text Heading 3.1 Some text Heading 3.1" & "Some text Heading 3.1 Some text Heading 3.1 Some text Heading 3.1")

			db.ParagraphFormat.Style = Heading2Style
			db.Writeln("Heading 3.2")
			db.ParagraphFormat.ClearFormatting()
			db.Writeln("Some text Heading 3.2 Some text Heading 3.2 Some text Heading 3.2 Some text Heading 3.2 Some text Heading 3.2")

			' Update the TOC field (table of contents).
			toe.Update()

			' Save our document into DOCX format.
			Dim resultPath As String = "result.docx"
			dc.Save(resultPath, New DocxSaveOptions())

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download

If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page).