Articles in this section
Category / Section

How to customize the TOC entries style in Word document?

4 mins read

Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can customize the TOC entries style in Word document in C#.

Steps to customize the TOC Entries style in the Word document in C#:

  1. Create a new C# .NET Core console application project. Create .NET Core console application in Visual Studio in ASP.NET Core Word
  2. Install the Syncfusion.DocIORenderer.NET.Core NuGet package as a reference to your .NET Core applications from NuGet.org. Add DocIORenderer NuGet packages of ASP.NET Core Word
  3. Include the following namespace in the Program.cs file:

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
  1. Use the following code example to customize the TOC entries style in the Word document:

C#

//Create a new Word document.
using (WordDocument document = new WordDocument())
 {
    document.EnsureMinimal();
    document.LastSection.PageSetup.Margins.All = 72;
    WParagraph para = document.LastParagraph;
    para.AppendText("Essential DocIO - Table of Contents");
    para.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
    para.ApplyStyle(BuiltinStyle.Heading4);
    para = document.LastSection.AddParagraph() as WParagraph;
    para = document.LastSection.AddParagraph() as WParagraph;
    //Insert TOC field in the Word document.
    TableOfContent toc = para.AppendTOC(1, 3);
    //Set the heading levels 1 to 3, to include in TOC.
    toc.LowerHeadingLevel = 1;
    toc.UpperHeadingLevel = 3;
    //Add content to the Word document with built-in heading styles.
    WSection section = document.LastSection;
    WParagraph newPara = section.AddParagraph() as WParagraph;
    newPara.AppendBreak(BreakType.PageBreak);
    AddHeading(section, BuiltinStyle.Heading1, "Document with built-in heading styles",        "This is the built-in heading 1 style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can insert TOC field in a word document. It can refresh or update TOC field by using UpdateTableOfContents method. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC.");
    AddHeading(section, BuiltinStyle.Heading2, "Section 1", "This is the built-in heading 2 style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks.");
    AddHeading(section, BuiltinStyle.Heading3, "Paragraph 1", "This is the built-in heading 3 style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text.");
    AddHeading(section, BuiltinStyle.Heading3, "Paragraph 2", "This is the built-in heading 3 style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph.");
    //Add a new section to the Word document.
    section = document.AddSection() as WSection;
    section.PageSetup.Margins.All = 72;
    section.BreakCode = SectionBreakCode.NewPage;
    AddHeading(section, BuiltinStyle.Heading2, "Section 2", "This is the built-in heading 2 style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks.");
    AddHeading(section, BuiltinStyle.Heading3, "Paragraph 1", "This is the built-in heading 3 style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text.");
    AddHeading(section, BuiltinStyle.Heading3, "Paragraph 2", "This is the built-in heading 3 style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph.");
    //Access the TOC 1 style and update its formatting.
    IWParagraphStyle toc1style = document.AddParagraphStyle("TOC 1");
    toc1style.CharacterFormat.FontName = "Calibri";
    toc1style.CharacterFormat.FontSize = 14;
    toc1style.CharacterFormat.Bold = true;
    toc1style.CharacterFormat.Italic = true;
    toc1style.ParagraphFormat.AfterSpacing = 8;
    //Access the TOC 2 style and update its formatting.
    IWParagraphStyle toc2style = document.AddParagraphStyle("TOC 2");
    toc2style.CharacterFormat.FontName = "Calibri";
    toc2style.CharacterFormat.FontSize = 12;
    toc2style.ParagraphFormat.AfterSpacing = 5;
    toc2style.CharacterFormat.Bold = true;
    toc2style.ParagraphFormat.LeftIndent = 11;
    //Access the TOC 3 style and update its formatting.
    IWParagraphStyle toc3style = document.AddParagraphStyle("TOC 3"); ;
    toc3style.CharacterFormat.FontName = "Calibri";
    toc3style.CharacterFormat.FontSize = 12;
    toc3style.ParagraphFormat.AfterSpacing = 3;
    toc3style.CharacterFormat.Italic = true;
    toc3style.ParagraphFormat.LeftIndent = 22;
    //Update the table of contents.
    document.UpdateTableOfContents();
    //Save the file in the given path.
    Stream docStream = File.Create(Path.GetFullPath(@"../../../Sample.docx"));
    document.Save(docStream, FormatType.Docx);
    docStream.Dispose();
 }
  1. Helper method to add a paragraph with specified heading style in the Word document.

C#

/// <summary>
/// Add Heading and Apply BuiltinStyle.
/// </summary>
private static void AddHeading(WSection section, BuiltinStyle builtinStyle, string headingText, string paragraghText)
{
    WParagraph newPara = section.AddParagraph() as WParagraph;
    WTextRange text = newPara.AppendText(headingText) as WTextRange;
    newPara.ApplyStyle(builtinStyle);
    newPara = section.AddParagraph() as WParagraph;
    newPara.AppendText(paragraghText);
    section.AddParagraph();
}

A complete working sample to customize TOC entries style in Word document in C# can be downloaded from GitHub.

By executing the program, you will get the Output document as follows:

Output document generated in ASP.NET Core Word

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail mergemerge 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.

See Also:

How to change the text of TOC Entries in the Table of Content in the Word document?

How to change tab leader of Table of Contents in Word document

Note:

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 link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied