How to change the text of TOC Entries in the Table of content in the Word document?
Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can change the text of TOC Entries in the Table of content in the Word document.
Steps to change the text of TOC Entries in the Table of content in the Word document programmatically:
- Create a new C# console application project.
- Install the Syncfusion.DocIO.Winforms NuGet package as a reference to your .NET Framework applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS;
VB.NET
Imports Syncfusion.DocIO Imports Syncfusion.DocIO.DLS
- Use the following code example to change the text of TOC Entries in the Table of content in the Word document.
C#
//Creates a new Word documentWordDocument 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 TableOfContent tableOfContents = 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(); ChangeTextofTOCEntries(tableOfContents.OwnerParagraph.OwnerTextBody, "First Chapter", "New Chapter"); //Saves and closes the Word document instance document.Save("Sample.docx", FormatType.Docx); document.Close();
VB.NET
'Creates a new Word document Dim document As WordDocument = 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 Dim tableOfContents As TableOfContent = 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() ChangeTextofTOCEntries(tableOfContents.OwnerParagraph.OwnerTextBody, "First Chapter", "New Chapter") 'Saves and closes the Word document instance document.Save("Sample.docx", FormatType.Docx) document.Close()
- Helper method to change the text of TOC Entries in the Table of content in the Word document
C#
/// <summary> /// Change the Text of TOC Entries /// </summary> /// <param name="textBody"></param> /// <param name="oldTOCText"></param> /// <param name="newTOCText"></param> private static void ChangeTextofTOCEntries(WTextBody textBody, string oldTOCText, string newTOCText) { for (int i = 0; i < textBody.ChildEntities.Count; i++) { if (textBody.ChildEntities[i] is WParagraph) { WParagraph paragraph = textBody.ChildEntities[i] as WParagraph; for (int j = 0; j < paragraph.ChildEntities.Count; j++) { if (paragraph.ChildEntities[j] is WField) { WField wf = (paragraph.ChildEntities[j] as WField); if (wf.FieldType == FieldType.FieldHyperlink) { if (wf.Text.Contains(oldTOCText)) { wf.Text = wf.Text.Replace(oldTOCText, newTOCText); } } } } } } }
VB.NET
'/ <summary> '/ Change the Text of TOC Entries '/ </summary> '/ <param name="textBody"></param> '/ <param name="oldTOCText"></param> '/ <param name="newTOCText"></param> Private Shared Sub ChangeTextofTOCEntries(ByVal textBody As WTextBody, ByVal oldTOCText As String, ByVal NewTOCText As String) Dim i As Integer For i = 0 To textBody.ChildEntities.Count - 1 Step i + 1 If TypeOf textBody.ChildEntities(i) Is WParagraph Then Dim paragraph As WParagraph = TryCast(textBody.ChildEntities(i), WParagraph) Dim j As Integer For j = 0 To paragraph.ChildEntities.Count - 1 Step j + 1 If TypeOf paragraph.ChildEntities(j) Is WField Then Dim wf As WField = (TryCast(paragraph.ChildEntities(j), WField)) If wf.FieldType = FieldType.FieldHyperlink Then If wf.Text.Contains(oldTOCText) Then wf.Text = wf.Text.Replace(oldTOCText, NewTOCText) End If End If End If Next End If Next End Sub
A complete working sample to change the text of TOC Entries in the Table of content in the Word document using C# can be downloaded from here
Take a moment to peruse the document where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion Word Framework features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about how to generate and register a Syncfusion license key in your application to use the components without a trail message.