Working with Table Of Contents
7 Aug 202324 minutes to read
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 the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the document
document.Close();
//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()
//DocIO supports Table of contents in WPF, Windows Forms platforms alone
You can download a complete working sample from GitHub.
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 and Windows Phone applications.
- Updating TOC makes use of the Word to PDF layout engine that may lead to update incorrect page number due to its limitations.
- In ASP.NET Core, Blazor, and Xamarin platforms, to update TOC in a Word document we recommend you to use Word to PDF assemblies or NuGet as a reference in your application.
The following code example shows how to update a TOC in an existing word document.
//Opens an existing document from stream through constructor of WordDocument class
FileStream fileStreamPath = new FileStream("Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic);
//Updates the table of contents.
document.UpdateTableOfContents();
//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the document
document.Close();
//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()
//DocIO supports Table of contents in WPF, Windows Forms platforms alone
//Please download the helper files from the below link to save the stream as file and open the file for viewing in Xamarin platform
//https://help.syncfusion.com/file-formats/docio/create-word-document-in-xamarin#helper-files-for-xamarin
You can download a complete working sample from GitHub.
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 the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the document
document.Close();
//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()
//DocIO supports Table of contents in WPF, Windows Forms platforms alone
You can download a complete working sample from GitHub.
Apply switches for table of contents
You can modify the preservation of TOC entries in a Word document by applying switches to the table of contents, and you can also apply switches to preserve TOC entries as hyperlinks or not, use the default heading styles, preserve page numbers or not, align the page numbers, preserve new line characters, preserve outline level paragraphs, and preserve table of entry fields in the TOC entries using Word (DocIO) library.
The following code example illustrates how to apply switches for the table of contents.
//Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add a section to the Word document.
IWSection section = document.AddSection();
//Add a paragraph to the created section.
IWParagraph paragraph = section.AddParagraph();
//Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determine the TOC entries.
TableOfContent tableOfContent = paragraph.AppendTOC(1, 3);
//Set lower heading level for TOC.
tableOfContent.LowerHeadingLevel = 2;
//Set upper heading level for TOC.
tableOfContent.UpperHeadingLevel = 5;
//Enable a flag to use default heading styles in TOC entries.
tableOfContent.UseHeadingStyles = true;
//Enable a flag to show page numbers in TOC entries.
tableOfContent.IncludePageNumbers = true;
//Disable a flag to align page numbers after the TOC entries.
tableOfContent.RightAlignPageNumbers = false;
//Disable a flag to preserve TOC entries without hyperlinks.
tableOfContent.UseHyperlinks = false;
//Add a paragraph to the section.
paragraph = section.AddParagraph();
//Append text.
paragraph.AppendText("First ");
//Append line break.
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendText("Chapter");
//Enable a flag to include newline characters in TOC entries.
tableOfContent.IncludeNewLineCharacters = true;
//Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading2);
//Add a section to the Word document.
section = document.AddSection();
//Add a paragraph to the section.
paragraph = section.AddParagraph();
//Append text.
paragraph.AppendText("Second ");
paragraph.AppendText("Chapter");
//Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading1);
//Add a section to the Word document.
section = document.AddSection();
paragraph = section.AddParagraph();
paragraph.AppendText("Third ");
paragraph.AppendText("Chapter");
//Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading2);
section = document.AddSection();
paragraph = section.AddParagraph();
paragraph.AppendText("Fourth ");
paragraph.AppendText("Chapter");
//Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading3);
//Add the text to the new paragraph of the section.
section.AddParagraph().AppendText("AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.");
//Add a paragraph to the section.
paragraph = section.AddParagraph();
//Set outline level for paragraph.
paragraph.ParagraphFormat.OutlineLevel = OutlineLevel.Level2;
//Append text.
paragraph.AppendText("Outline Level Paragraph");
//Enable a flag to consider outline level paragraphs in TOC entries.
tableOfContent.UseOutlineLevels = true;
//Add a section to the Word document.
section = document.AddSection();
//Add a paragraph to the section.
paragraph = section.AddParagraph();
//Append a field to the paragraph.
paragraph.AppendField("Table of Entry Field", FieldType.FieldTOCEntry);
//Enable a flag to use table entry fields in TOC entries.
tableOfContent.UseTableEntryFields = true;
//Update the table of contents.
document.UpdateTableOfContents();
//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the document
document.Close();
}
//Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add a section to the Word document.
IWSection section = document.AddSection();
//Add a paragraph to the created section.
IWParagraph paragraph = section.AddParagraph();
//Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determine the TOC entries.
TableOfContent tableOfContent = paragraph.AppendTOC(1, 3);
//Set lower heading level for TOC.
tableOfContent.LowerHeadingLevel = 2;
//Set upper heading level for TOC.
tableOfContent.UpperHeadingLevel = 5;
//Enable a flag to use default heading styles in TOC entries.
tableOfContent.UseHeadingStyles = true;
//Enable a flag to show page numbers in TOC entries.
tableOfContent.IncludePageNumbers = true;
//Disable a flag to align page numbers after the TOC entries.
tableOfContent.RightAlignPageNumbers = false;
//Disable a flag to preserve TOC entries without hyperlinks.
tableOfContent.UseHyperlinks = false;
//Add a paragraph to the section.
paragraph = section.AddParagraph();
//Append text.
paragraph.AppendText("First ");
//Append line break.
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendText("Chapter");
//Enable a flag to include newline characters in TOC entries.
tableOfContent.IncludeNewLineCharacters = true;
//Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading2);
//Add a section to the Word document.
section = document.AddSection();
//Add a paragraph to the section.
paragraph = section.AddParagraph();
//Append text.
paragraph.AppendText("Second ");
paragraph.AppendText("Chapter");
//Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading1);
//Add a section to the Word document.
section = document.AddSection();
paragraph = section.AddParagraph();
paragraph.AppendText("Third ");
paragraph.AppendText("Chapter");
//Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading2);
section = document.AddSection();
paragraph = section.AddParagraph();
paragraph.AppendText("Fourth ");
paragraph.AppendText("Chapter");
//Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading3);
//Add the text to the new paragraph of the section.
section.AddParagraph().AppendText("AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.");
//Add a paragraph to the section.
paragraph = section.AddParagraph();
//Set outline level for paragraph.
paragraph.ParagraphFormat.OutlineLevel = OutlineLevel.Level2;
//Append text.
paragraph.AppendText("Outline Level Paragraph");
//Enable a flag to consider outline level paragraphs in TOC entries.
tableOfContent.UseOutlineLevels = true;
//Add a section to the Word document.
section = document.AddSection();
//Add a paragraph to the section.
paragraph = section.AddParagraph();
//Append a field to the paragraph.
paragraph.AppendField("Table of Entry Field", FieldType.FieldTOCEntry);
//Enable a flag to use table entry fields in TOC entries.
tableOfContent.UseTableEntryFields = true;
//Update the table of contents.
document.UpdateTableOfContents();
//Save a Word document.
document.Save("Sample.docx");
}
'Create a new Word document.
Using document As WordDocument = New WordDocument()
'Add a section into the Word document.
Dim section As IWSection = document.AddSection()
'Add a paragraph into the created section.
Dim paragraph As IWParagraph = section.AddParagraph()
'Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determines the TOC entries.
Dim tableOfContent As TableOfContent = paragraph.AppendTOC(1, 3)
'Set lower heading level for TOC.
tableOfContent.LowerHeadingLevel = 2
'Set upper heading level for TOC.
tableOfContent.UpperHeadingLevel = 5
'Enable a flag to use default heading styles in TOC entries.
tableOfContent.UseHeadingStyles = True
'Enable a flag to show page numbers in TOC entries.
tableOfContent.IncludePageNumbers = True
'Disable a flag to align page numbers after to the TOC entries.
tableOfContent.RightAlignPageNumbers = False
'Disable a flag to preserve TOC entries without hyperlinks.
tableOfContent.UseHyperlinks = False
'Add a paragraph into the section.
paragraph = section.AddParagraph()
'Append text.
paragraph.AppendText("First ")
'Append line break.
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendText("Chapter")
'Enable a flag to include new line characters in TOC entries.
tableOfContent.IncludeNewLineCharacters = True
'Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading2)
'Add a section into the Word document.
section = document.AddSection()
'Add a paragraph to the section.
paragraph = section.AddParagraph()
'Append text.
paragraph.AppendText("Second ")
paragraph.AppendText("Chapter")
'Sets a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading1)
'Add a section into the Word document.
section = document.AddSection()
paragraph = section.AddParagraph()
paragraph.AppendText("Third ")
paragraph.AppendText("Chapter")
'Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading2)
section = document.AddSection()
paragraph = section.AddParagraph()
paragraph.AppendText("Fourth ")
paragraph.AppendText("Chapter")
'Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading3)
'Add the text into the new paragraph of the section.
section.AddParagraph().AppendText("AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.")
'Add a paragraph to the section.
paragraph = section.AddParagraph()
'Set outline level for paragraph.
paragraph.ParagraphFormat.OutlineLevel = OutlineLevel.Level2
'Append text.
paragraph.AppendText("Outline Level Paragraph")
'Enable a flag to consider outline level paragraphs in TOC entries.
tableOfContent.UseOutlineLevels = True
'Add a section into the Word document.
section = document.AddSection()
'Add a paragraph to the section.
paragraph = section.AddParagraph()
'Append a field to the paragraph.
paragraph.AppendField("Table of Entry Field", FieldType.FieldTOCEntry)
'Enable a flag to use table entry fields in TOC entries.
tableOfContent.UseTableEntryFields = True
'Update the table of content.
document.UpdateTableOfContents()
'Save a Word document.
document.Save("Sample.docx")
End Using
//DocIO doesn't support updating table of contents in UWP platform.