Click or drag to resize

DocumentBuilderInsertTableOfContents Method

Inserts a table of contents into the document.

Namespace: SautinSoft.Document
Assembly: SautinSoft.Document (in SautinSoft.Document.dll) Version: 2025.2.13
Syntax
public TableOfEntries InsertTableOfContents(
	string switches
)

Parameters

switches  String
The Microsoft Word TOC field switches.

Return Value

TableOfEntries
TableOfEntries that was just inserted.
Remarks

The TOC field collects entries for a table of contents using heading levels, specified styles, or entries specified by TC (Table of Contents Entry) fields.

If InstructionText is not specified Update method will set a default set of switches: \o "1-3" \h \u \z.

Supported TOC field switches:

\o "Headings"
Builds a table of contents from paragraphs formatted with built-in heading styles. For example, { TOC \o "1-3" } lists only headings formatted with the styles Heading 1 through Heading 3. If no heading range is specified, all heading levels used in the document are listed. Enclose the range numbers in quotation marks.

\u
Builds a table of contents from paragraphs formatted with all built-in heading styles (1-9).

\f EntryIdentifier
Builds a table from TC fields. If EntryIdentifier is specified, the table is built only from TC fields with the same identifier (typically a letter). For example, { TOC \f t } builds a table of contents from TC fields such as { TC "Entry Text" \f t }.

\h
Inserts TOC entries as hyperlinks.

\n Levels
Omits page numbers from the table of contents. Page numbers are omitted from all levels unless a range of entry levels is specified. For example, { TOC \n 3-4 } omits page numbers from levels 3 and 4. Delete this switch to include page numbers.

\l Levels
Builds a table of contents from TC fields that assign entries to one of the specified levels. For example, { TOC \l 1-4 } builds a table of contents from TC fields that assign entries to levels 1-4. TC fields that assign entries to lower levels are skipped.

\p "Separators"
Specifies the characters that separate an entry and its page number. For example, the field { TOC \p "—" }, with an em dash, displays a result such as "Selecting Text—53." The default is a tab with leader dots. You can use up to five characters, which must be enclosed in quotation marks.

Supported TC field instructions:

"Text"
Text to appear in the table of contents for an entry.

Supported TC field switches:

\f Type
The type of items collected in a particular contents list. Use a unique Type identifier (typically a letter from A-Z) for each type of list. For example, to build a list of illustrations, mark each illustration with a field such as { TC "Illustration 1" \f i }, where "i" indicates only illustration entries. If no type is specified, the entry is listed in a table of contents.

\l Level
The level of the TC entry. For example, the field { TC "Entering Data" \l 4 } marks a level-4 entry, and Microsoft Word applies the built-in style TOC 4 to that entry in the table of contents. If no level is specified, level 1 is assumed.

\n
Omits the page number for the entry.

For more information about TOC and TC fields, see Field codes in Word

Example

See Developer Guide: How to insert a TOC (Table of Contents) field into a document using DocumentBuilder

How to insert a TOC (Table of Contents) field into a document using DocumentBuilder in C#
using System;
using SautinSoft.Document;
using System.Text;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free trial 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());

            // Important for Linux: Install MS Fonts
            // sudo apt install ttf-mscorefonts-installer -y

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
        }
    }
}
How to insert a TOC (Table of Contents) field into a document using DocumentBuilder in VB.Net
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 trial 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
See Also