Back to Devexpress

How to: Create a Table of Contents

officefileapi-120710-word-processing-document-api-examples-document-elements-how-to-create-a-table-of-contents.md

latest10.1 KB
Original Source

How to: Create a Table of Contents

  • Mar 28, 2025
  • 5 minutes to read

This topic describes how to create a table of contents of different types.

To create a TOC, perform the following steps.

  1. Mark the Entries

  2. Build the Table of Contents

Refer to the following GitHub repository for a complete sample project:

View Example: Word Processing Document API - Generate a Table Of Contents (TOC)

Mark the Entries

Word Document API ships with the following approaches to mark a document text (heading) to include it into the table of contents.

Outline Levels

Set the outline level to paragraphs that should be included into the table of contents. To do that, use the Paragraph.OutlineLevel property as shown in the following code snippet. Note that the maximum outline level is 9.

csharp
using (var wordProcessor = new RichEditDocumentServer()) {
wordProcessor.LoadDocument("Documents//Table of Contents.docx");

// Set the outline level to every chapter title in the document
for (int i = 0; i < wordProcessor.Document.Paragraphs.Count; i++) {
    string var = wordProcessor.Document.GetText(wordProcessor.Document.Paragraphs[i].Range);
    if (var.Contains("CHAPTER "))
    {
        wordProcessor.Document.Paragraphs[i].OutlineLevel = 2;
    }
  }
}
vb
Using wordProcessor = New RichEditDocumentServer()

    wordProcessor.LoadDocument("Documents//Table of Contents.docx")

    ' Set the outline level to every chapter title in the document
    For i As Integer = 0 To wordProcessor.Document.Paragraphs.Count - 1
        Dim var As String = wordProcessor.Document.GetText(wordProcessor.Document.Paragraphs(i).Range)
        If var.Contains("CHAPTER ") Then
            wordProcessor.Document.Paragraphs(i).OutlineLevel = 2
        End If
    Next i
End Using

Heading Styles

Create a new heading style or retrieve an existing one from the ParagraphStyleCollection collection (can be obtained by the Document.ParagraphStyles property) and apply it to the target paragraphs. For that, use the ParagraphProperties.Style property as shown in the following code snippet.

csharp
using (var wordProcessor = new RichEditDocumentServer()) {
wordProcessor.LoadDocument("Documents//Table of Contents.docx");

// Apply the "heading 1" style to every chapter title in the document
for (int i = 0; i < wordProcessor.Document.Paragraphs.Count; i++) {
    string var = wordProcessor.Document.GetText(wordProcessor.Document.Paragraphs[i].Range);
    if (var.Contains("CHAPTER "))
    {
        wordProcessor.Document.Paragraphs[i].Style = wordProcessor.Document.ParagraphStyles["heading 1"];
    }
  }
}
vb
Using wordProcessor = New RichEditDocumentServer()
wordProcessor.LoadDocument("Documents//Table of Contents.docx")

' Apply the "heading 1" style to every chapter title in the document
For i As Integer = 0 To wordProcessor.Document.Paragraphs.Count - 1
    Dim var As String = wordProcessor.Document.GetText(wordProcessor.Document.Paragraphs(i).Range)
    If var.Contains("CHAPTER ") Then
        wordProcessor.Document.Paragraphs(i).Style = wordProcessor.Document.ParagraphStyles("heading 1")
    End If
Next i
End Using

TC Fields

Insert a TC field at the beginning of the target paragraph. Type the caption in the field switch, and the document displays the caption in the table of contents instead of the original title.

csharp
using (var wordProcessor = new RichEditDocumentServer()) {
wordProcessor.LoadDocument("Documents//Table of Contents.docx");
int j = 1;

// Mark every chapter title in the document by the TC field
for (int i = 0; i < wordProcessor.Document.Paragraphs.Count; i++) {
    string var = wordProcessor.Document.GetText(wordProcessor.Document.Paragraphs[i].Range);

    if (var.Contains("CHAPTER ")) {
        Field field = wordProcessor.Document.Fields.Create(wordProcessor.Document.Paragraphs[i].Range.Start, String.Format("TC {0} \\f bvz ", j));
        wordProcessor.Document.Fields.Update();
        j++;
    }
  } 
}
vb
Using wordProcessor = New RichEditDocumentServer()
wordProcessor.LoadDocument("Documents//Table of Contents.docx")

Dim j As Integer = 1

' Mark every chapter title in the document by the TC field
For i As Integer = 0 To wordProcessor.Document.Paragraphs.Count - 1
    Dim var As String = wordProcessor.Document.GetText(wordProcessor.Document.Paragraphs(i).Range)

    If var.Contains("CHAPTER ") Then
        Dim field As Field = wordProcessor.Document.Fields.Create(wordProcessor.Document.Paragraphs(i).Range.Start, String.Format("TC {0} \f bvz ", j))
        wordProcessor.Document.Fields.Update()
        j += 1
    End If
Next i
End Using

SEQ Fields

Use these fields to mark the captions of document figures/tables/equations and include them into the corresponding table. Insert a caption into the target figure and mark it with the SEQ field as shown in the following code snippet.

csharp
using (var wordProcessor = new RichEditDocumentServer()) {
    wordProcessor.LoadDocument("Documents//Table of Contents.docx");
    Document document = wordProcessor.Document;
    document.BeginUpdate();

    for (int i = 0; i < document.Images.Count; i++) {
        DocumentImage shape = document.Images[i];
        Paragraph paragraph = document.Paragraphs.Insert(shape.Range.End);

        // Insert caption to every image in the document
        DocumentRange range = document.InsertText(paragraph.Range.Start, "Image ");

        // Mark captions with the SEQ fields
        Field field = document.Fields.Create(range.End, "SEQ Image \\*ARABIC");
    }
    document.Fields.Update();
    document.EndUpdate();
}
vb
Using wordProcessor = New RichEditDocumentServer()
    wordProcessor.LoadDocument("Documents//Table of Contents.docx")
    Dim document As Document = wordProcessor.Document
    document.BeginUpdate()

    For i As Integer = 0 To document.Images.Count - 1
        Dim shape As DocumentImage = document.Images(i)
        Dim paragraph As Paragraph = document.Paragraphs.Insert(shape.Range.End)

        'Insert caption to every image in the document
        Dim range As DocumentRange = document.InsertText(paragraph.Range.Start, "Image ")

        'Mark the captions with the SEQ fields
        Dim field As Field = document.Fields.Create(range.End, "SEQ Image \*ARABIC")
    Next i
    document.Fields.Update()
    document.EndUpdate()
End Using

Build the Table of Contents

Insert the TOC field with the corresponding switch into the desired position within the document using the following API.

MemberDescription
SubDocument.BeginUpdate()Initialize the document modification.
SubDocument.ParagraphsObtains the target paragraph.
FieldCollection.CreateInserts a field into the given position within the document.

Depending on the selected approach, use one of the following field switches.

* \u - for outline levels
* none - for heading styles
* \f identifier - for TC fields
* \c identifier - for SEQ fields | | Field.Update() | Updates the given field. | | SubDocument.EndUpdate | Finalizes the document update. |

Tip

To insert all TOC entries as hyperlinks, use the \h switch. You can use other TOC field switches to modify any kind of TOC to fit your needs.

The following code snippet inserts a TOC field into the given document position.

csharp
using (var wordProcessor = new RichEditDocumentServer()) {
    wordProcessor.LoadDocument("Documents//Table of Contents.docx");
    Document document = wordProcessor.Document;
    document.BeginUpdate();
    Paragraph paragraph = document.Paragraphs.Insert(document.Paragraphs[1].Range.Start);

    //Insert the table of contents to the beginning of the document
    document.InsertText(paragraph.Range.Start, "Table of Contents (Heading Styles) " + "\r");
    Field field = document.Fields.Create(paragraph.Range.Start, "TOC \\h ");
    field.Update();

    //Update all fields to apply changes
    document.Fields.Update();
    document.EndUpdate();
}
vb
Using wordProcessor = New RichEditDocumentServer()
    wordProcessor.LoadDocument("Documents//Table of Contents.docx")

    Dim document As Document = richEditDocumentServer1.Document
    document.BeginUpdate()
    Dim paragraph As Paragraph = document.Paragraphs.Insert(document.Paragraphs(1).Range.Start)

    'Insert the table of contents to the beginning of the document
    document.InsertText(paragraph.Range.Start, "Table of Contents (Heading Styles) " & ControlChars.Cr)
    Dim field As Field = document.Fields.Create(paragraph.Range.Start, "TOC \h ")
    field.Update()

    'Update all fields to apply changes
    document.Fields.Update()
    document.EndUpdate()
End Using