Products Technologies Demo Docs Blog Support Company

Creating Table of Contents without Stylesheets

Table of contents are created automatically based on used stylesheets for the different structure levels. This sample shows how to convert paragraphs to stylesheets to insert a table of contents.

Creating Table of Contents without Stylesheets

Stylesheet Selection

TX Text Control X19 is able to generate table of contents automatically based on used styles in a document. In the ribbon tab References, the styles, that should be included in the table of contents, can be selected:

TOC in TX Text Control

In the Table of Contents dialog, the minimum and maximum structure levels can be defined in order to add a newly generated table of contents at the current input position.

TOC in TX Text Control

Generate Stylesheets

Now consider an unstructured document without used stylesheets. The user would have to create and apply styles first, before a table of contents can be generated. This sample shows how to convert an existing formatting to a style and how to apply this style to all similar paragraphs with the same layout.

The following screenshot shows a document without any stylesheets. But visually, the headings (in bold) are formatted in a different way:

TOC in TX Text Control

The following extension method converts the formatting at the current input position into a ParagraphStyle and returns the newly created style name:

public static string CreateStyleFromSelection(this TXTextControl.TextControl textControl) {

   // get current input position
   var iParStart = textControl.Paragraphs.GetItem(
      textControl.InputPosition.TextPosition).Start;
   textControl.Select(iParStart, 0);

   // create a new paragraph style based on current formatting
   TXTextControl.ParagraphStyle parStyle = 
      new TXTextControl.ParagraphStyle("custom_" + Guid.NewGuid().ToString());

   // set style
   parStyle.Baseline = textControl.Selection.Baseline;
   parStyle.Bold = textControl.Selection.Bold;
   parStyle.FontName = textControl.Selection.FontName;
   parStyle.FontSize = textControl.Selection.FontSize;
   parStyle.ForeColor = textControl.Selection.ForeColor;
   parStyle.Italic = textControl.Selection.Italic;
   parStyle.Strikeout = textControl.Selection.Strikeout;
   parStyle.TextBackColor = textControl.Selection.TextBackColor;
   parStyle.Underline = textControl.Selection.Underline;

   // add style to TextControl
   textControl.ParagraphStyles.Add(parStyle);

   // return the style name
   return parStyle.Name;
}

The next extension method applies this style to all paragraphs with the same formatting:

public static void CompareAndApplyStyle(
   this TXTextControl.TextControl textControl, 
   string paragraphStyleName) {

   // store input position
   var iStartPos = textControl.Selection.Start;

   // retrieve the style based on a name
   TXTextControl.ParagraphStyle style = 
      textControl.ParagraphStyles.GetItem(paragraphStyleName);

   // loop through all paragraphs to check whether the style
   // matches the paragraph style
   foreach (TXTextControl.Paragraph par in textControl.Paragraphs) {

      textControl.Select(par.Start, 0);
      var selection = textControl.Selection;

      if (selection.Baseline == style.Baseline &&
         selection.Bold == style.Bold &&
         selection.FontName == style.FontName &&
         selection.FontSize == style.FontSize &&
         selection.ForeColor == style.ForeColor &&
         selection.Italic == style.Italic &&
         selection.Strikeout == style.Strikeout &&
         selection.Underline == style.Underline) {

         // style matches - apply style
         par.FormattingStyle = paragraphStyleName;
      }

   }

   // reset input position
   textControl.Selection.Start = iStartPos;
}

In the sample, a new button is added to the References tab that calls the extension methods:

TOC in TX Text Control

private void RbMyButton_Click(object sender, EventArgs e) {
     // create a style
     var styleName = textControl1.CreateStyleFromSelection();

     // apply style
     textControl1.CompareAndApplyStyle(styleName);
}

Create the Table of Contents

Now, the new style can be selected as a structure level:

TOC in TX Text Control

And after that, a table of contens can be added based on the newly created styles:

TOC in TX Text Control

Feel free to download the sample from our GitHub repository and test this on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.TableOfContents Class

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • TX Text Control .NET for Windows Forms X19
  • Visual Studio 2019

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ActiveXASP.NETWindows Forms

Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5

TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…


ASP.NETWindows FormsWPF

The Wait is Over: TX Text Control for Linux is Officially Here

We are very excited to announce the release of TX Text Control 33.0 which includes the long awaited Linux version of TX Text Control. This version allows you to integrate TX Text Control into your…


ASP.NETWindows FormsWPF

Full .NET 9 Support in Text Control .NET Components for ASP.NET Core,…

.NET 9 will be launched tomorrow, November 12, at the .NET Conf 2024 with updates to cloud capabilities, security, and performance. TX Text Control .NET components are fully compatible with .NET 9…