Working with Table Of Contents

Table of contents (TOC) is used to provide an outline of the Word document. By default table of contents will be created automatically from heading styles.

You can add the TOC into the paragraph by specifying the LowerHeadingLevel and UpperHeadingLevel. The heading level range must be from 1 to 9.

Basically TOC determines the TOC entries based on the TOC switches.

S.No

Switches

Description

1

\o

Builds a table of contents from paragraphs formatted with built-in heading styles.

2

\h

Inserts TOC entries as hyperlinks.

3

\n

Omits page numbers from the table of contents.

4

\p

Specifies the characters that separate a TOC entry and its page number. The default is a tab with leader dots.

5

\t

Builds a table of contents from paragraphs formatted with specified styles other than the built-in heading styles

Adding a TOC field

The following code example shows how to add a table of contents (TOC) in Word document.

//Creates a new Word document

WordDocument document = new WordDocument();

//Adds 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.";

//Adds the paragraph into the created section

IWParagraph paragraph = section.AddParagraph();

//Appends the TOC field with LowerHeadingLevel and UpperHeadingLevel to determines the TOC entries

paragraph.AppendTOC(1, 3);

//Adds the section into the Word document

section = document.AddSection();

//Adds the paragraph into the created section

paragraph = section.AddParagraph(); 

//Adds the text for the headings

paragraph.AppendText("First Chapter");

//Sets a built-in heading style.

paragraph.ApplyStyle(BuiltinStyle.Heading1);

//Adds the text into the paragraph

section.AddParagraph().AppendText(paraText);

//Adds the section into the Word document

section = document.AddSection();

//Adds the paragraph into the created section

paragraph = section.AddParagraph();

//Adds the text for the headings

paragraph.AppendText("Second Chapter");

//Sets a built-in heading style.

paragraph.ApplyStyle(BuiltinStyle.Heading2);

//Adds the text into the paragraph

section.AddParagraph().AppendText(paraText);

//Adds the section into the Word document

section = document.AddSection();

//Adds the paragraph into the created section

paragraph = section.AddParagraph();

//Adds the text into the headings

paragraph.AppendText("Third Chapter");

//Sets a built-in heading style

paragraph.ApplyStyle(BuiltinStyle.Heading3);

//Adds the text into the paragraph.

section.AddParagraph().AppendText(paraText);

//Updates the table of contents

document.UpdateTableOfContents();

//Saves and closes the Word document instance

document.Save("Sample.docx", FormatType.Docx);

document.Close();
'Creates a new Word document

Dim document As New WordDocument()

'Adds the section into the Word document

Dim section As IWSection = document.AddSection()

Dim paraText As String = "AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company."

'Adds the paragraph into the created section

Dim paragraph As IWParagraph = section.AddParagraph()

'Appends the TOC field with LowerHeadingLevel and UpperHeadingLevel to determines the TOC entries

paragraph.AppendTOC(1, 3)

'Adds the section into the Word document

section = document.AddSection()

'Adds the paragraph into the created section

paragraph = section.AddParagraph()

'Adds the text for the headings

paragraph.AppendText("First Chapter")

'Sets a built-in heading style

paragraph.ApplyStyle(BuiltinStyle.Heading1)

'Adds the text into the paragraph.

section.AddParagraph().AppendText(paraText)

'Adds the section into the Word document

section = document.AddSection()

'Adds the paragraph into the created section

paragraph = section.AddParagraph()

'Adds the text for the headings

paragraph.AppendText("Second Chapter")

'Sets a built-in heading style

paragraph.ApplyStyle(BuiltinStyle.Heading2)

'Adds the text into the paragraph

section.AddParagraph().AppendText(paraText)

'Adds the section into the Word document

section = document.AddSection()

'Adds the paragraph into the created section

paragraph = section.AddParagraph()

'Adds the text into the headings

paragraph.AppendText("Third Chapter")

'Sets a built-in heading style

paragraph.ApplyStyle(BuiltinStyle.Heading3)

'Adds the text into the paragraph

section.AddParagraph().AppendText(paraText)

'Updates the table of contents

document.UpdateTableOfContents()

Saves and closes the Word document instance

document.Save("Sample.docx", FormatType.Docx)

document.Close()

Updating table of contents

You can also update or re-build the TOC in an existing document or document created from the scratch.

NOTE

Updating of TOC is not supported in Silverlight, WinRT, Universal, Xamarin and Windows Phone applications using DocIO.

Updating TOC makes use of the Word to PDF layout engine that may lead to update incorrect page number due to its limitations.

The following code example shows how to update a TOC in an existing word document.

//Opens an input word template

WordDocument document = new WordDocument(@”Template.docx);

//Updates the table of contents.

document.UpdateTableOfContents();

//Saves and closes the Word document instance.

document.Save("Sample.docx", FormatType.Docx);

document.Close();
'Opens an input word template

Dim document As New WordDocument("Template.docx")

'Updates the table of contents.

document.UpdateTableOfContents()

Saves and closes the Word document instance.

document.Save("Sample.docx", FormatType.Docx)

document.Close()

Creating table of contents with user-defined styles

The following code example shows how to create table of contents with user-defined styles instead of heading styles.

//Creates a new Word document

WordDocument document = new WordDocument();

//Creates a new custom styles

Style style = (WParagraphStyle)document.AddParagraphStyle("MyStyle");

style.CharacterFormat.Bold = true;

style.CharacterFormat.FontName = "Verdana";

style.CharacterFormat.FontSize = 25;

//Adds 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.";

//Adds the paragraph into the created section

IWParagraph paragraph = section.AddParagraph();

//Appends the TOC field with LowerHeadingLevel and UpperHeadingLevel to determines the TOC entries

TableOfContent tableOfContents = paragraph.AppendTOC(1, 3);

tableOfContents.UseHeadingStyles = false;

//Sets the TOC level style based on the created TOC 

tableOfContents.SetTOCLevelStyle(2, "MyStyle");

//Adds the section into the Word document

section = document.AddSection();

//Adds the paragraph into the created section

paragraph = section.AddParagraph();

//Adds the text for the headings

paragraph.AppendText("First Chapter");

//Sets the built-in heading style

paragraph.ApplyStyle("MyStyle");

//Adds the text into the paragraph

section.AddParagraph().AppendText(paraText);

//Adds the section into the Word document

section = document.AddSection();

//Adds the paragraph into the created section

paragraph = section.AddParagraph();

//Adds the text for the headings

paragraph.AppendText("Second Chapter");

//Sets the built-in heading style

paragraph.ApplyStyle(BuiltinStyle.Heading1);

//Adds the text to the paragraph

section.AddParagraph().AppendText(paraText);

//Adds the section into Word document

section = document.AddSection();

//Adds a paragraph to a created section

paragraph = section.AddParagraph();

//Adds the text for the headings

paragraph.AppendText("Third Chapter");

//Sets the built-in heading style

paragraph.ApplyStyle("MyStyle");

//Adds the text to the paragraph

section.AddParagraph().AppendText(paraText);

//Updates the table of contents

document.UpdateTableOfContents();

//Saves and closes the Word document instance

document.Save("Sample.docx", FormatType.Docx);

document.Close();
'Creates a new Word document

Dim document As New WordDocument()

'Creates a new custom styles

Dim style As Style = DirectCast(document.AddParagraphStyle("MyStyle"), WParagraphStyle)

style.CharacterFormat.Bold = True

style.CharacterFormat.FontName = "Verdana"

style.CharacterFormat.FontSize = 25

'Adds the section into the Word document

Dim section As IWSection = document.AddSection()

Dim paraText As String = "AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company."

'Adds the paragraph into the created section

Dim paragraph As IWParagraph = section.AddParagraph()

'Appends the TOC field with LowerHeadingLevel and UpperHeadingLevel to determine the TOC entries

Dim tableOfContents As TableOfContent = paragraph.AppendTOC(1, 3)

tableOfContents.UseHeadingStyles = False

'Sets the TOC level style based on the created TOC 

tableOfContents.SetTOCLevelStyle(2, "MyStyle")

'Adds the section into the Word document

section = document.AddSection()

'Adds the paragraph into the created section

paragraph = section.AddParagraph()

'Adds the text for the headings

paragraph.AppendText("First Chapter")

'Sets the built-in heading style

paragraph.ApplyStyle("MyStyle")

'Adds the text into the paragraph

section.AddParagraph().AppendText(paraText)

'Adds the section into the Word document

section = document.AddSection()

'Adds the paragraph into the created section

paragraph = section.AddParagraph()

'Adds the text for the headings

paragraph.AppendText("Second Chapter")

'Sets the built-in heading style

paragraph.ApplyStyle(BuiltinStyle.Heading1)

'Adds the text to the paragraph

section.AddParagraph().AppendText(paraText)

'Adds the section into Word document

section = document.AddSection()

'Adds a paragraph to created section

paragraph = section.AddParagraph()

'Adds the text for the headings

paragraph.AppendText("Third Chapter")

'Sets the built-in heading style

paragraph.ApplyStyle("My style")

'Adds the text to the paragraph

section.AddParagraph().AppendText(paraText)

'Updates the table of contents

document.UpdateTableOfContents()

Saves and closes the Word document instance

document.Save("Sample.docx", FormatType.Docx)

document.Close()