Using TX Text Control .NET for Windows Forms, a table of contents can be easily created using very simple steps.
Creating a TOC using the Ribbon
First, we have to understand the concept of styles. A style can be used to apply several formatting options at once to a paragraph or part of text. This could be font settings such as family or size and other attributes such as bold, italic etc. Now we want to define a unique Paragraph
╰ TXTextControl Namespace
╰ ParagraphStyle Class
The ParagraphStyle class defines a formatting style for paragraphs. for the headings of each chapter. The headings will be the elements listed in our TOC.
- Open the Styles side bar
- Click Manage Styles
- Create a new style called "heading1"
- Apply the style to our heading
The next step is to define the Structure
╰ TXTextControl Namespace
╰ ParagraphFormat Class
╰ StructureLevel Property
Gets or sets the structure level of a paragraph in the document. for different parts of the document. For example the body of the document has level 0. The table of contents then consists of the levels that can be chosen in the References tab of the ribbon.
In the References tab, go to the Paragraph Structure Levels group. Then find "heading1", and select Level 1 from the dropdown.
Now go to the very beginning of your document and choose Insert from the Table of Contents group. Then choose Level 1 for the minimum and maximum structure level and confirm with Ok. Voila! A table of contents consisting of the chosen headings is generated automatically.
Creating a TOC in C#
Of course this can be achieved programmatically as well with only a few steps. To create and apply a Paragraph
╰ TXTextControl Namespace
╰ ParagraphStyle Class
The ParagraphStyle class defines a formatting style for paragraphs. to a text selection see the following snippet.
TXTextControl.ParagraphStyle par = new TXTextControl.ParagraphStyle("heading1"); | |
par.Bold = true; | |
par.FontSize = 400; | |
par.FontName = "Arial"; | |
textControl1.ParagraphStyles.Add(par); | |
textControl1.Selection.FormattingStyle = "heading1"; |
The advantage is that fewer steps are required, because the structure level can already be defined in the style itself. The complete code looks like this:
TXTextControl.ParagraphStyle par = new TXTextControl.ParagraphStyle("heading1"); | |
par.Bold = true; | |
par.FontSize = 400; | |
par.FontName = "Arial"; | |
par.ParagraphFormat.StructureLevel = 1; | |
textControl1.ParagraphStyles.Add(par); | |
textControl1.Selection.FormattingStyle = "heading1"; |
To add a new table of contents to the document, the object must be created and added to the Table
╰ TXTextControl Namespace
╰ TableOfContentsCollection Class
Contains all tables of contents in a certain part of the document. .
var toc = new TableOfContents(1, 2); | |
toc.ID = id; | |
toc.Title = "Table of Contents"; | |
id++; | |
textControl1.TablesOfContents.Add(toc); |
What happens during the creation is that the document is searched for all paragraphs with the specified structure levels, generates the table of contents with or without page numbers and adds it at the current text input position.