
An often requested feature of TX Text Control is a dynamically created table of contents (TOC) like offered in MS Word. Based on the style interface of TX Text Control, this is possible and not that tricky.
This sample shows how to use the FormattingStyles to build a list of paragraphs that are part of the TOC. The toc class of this sample provides some public values to configure the table of contents. The following code creates a new TOC and inserts it at the current input position:
TableOfContents toc = new TableOfContents(textControl1);
toc.MatchList = new string[]
{ "Heading 1", "Heading 2",};
toc.Title = "Table of Contents";
toc.RightAlignPageNumbers = true;
toc.ShowPageNumbers = true;
toc.Create();The MatchList string array contains all FormattingStyles that should be used to create the TOC. The order is given thru the order in the array: The first entry is level 1, the second is level 2 and so on.
Now, all paragraphs will be iterated to match the used style with this match list. If a style matches, a target will be inserted at this position. In a later process, all targets will be collected and inserted as hyperlinks at the current input position. These links are connected to the targets in the document, so that when a user clicks on a link, TX Text Control scrolls to that position.
When all links are inserted, all targets will be iterated again to get the current page numbers. This must be done at the end, because we possibly insert the TOC at the beginning of the page which causes the targets to shift.
The complete TOC block will be encapsulated in separate targets to be able to remove the complete TOC from the document.
The minimum requirements for this sample application are TX Text Control .NET trial and Visual Studio .NET 2005.