Combining MailMerge and Table of Contents
MailMerge is used to merge data into templates from JSON data or IEnumerable objects. This article explains how to combine table of contents with dynamically generated documents using the MailMerge reporting engine.

The table of contents element can be used to list entries that are part of mail merge templates and in particular in dynamic merge blocks that are generated with data from JSON data or business objects.
MailMerge Template
Consider the following template that contains the title page with a table of contents element that should include all entries with the style name Heading.
On the second page, there is a repeating merge block that uses the selected style Heading. For each entry of this merge block in the final document, an entry should be available in the table of contents.
Update TOC
After the document has been merged using the Mail
textControl1.Load("block_toc_template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
using (TXTextControl.DocumentServer.MailMerge mm =
new TXTextControl.DocumentServer.MailMerge()) {
mm.TextComponent = textControl1;
mm.MergeJsonData(System.IO.File.ReadAllText("data.json"));
}
foreach (TXTextControl.TableOfContents toc in textControl1.TablesOfContents) {
toc.Update();
}
After the table of contents have been updated, two entries are shown from the repeated merge blocks on page 2 and 3:
Download Samples
The templates from this article and the data source can be downloaded for your own tests using TX Text Control.
Download Sample Templates
For your own tests, you can download a set of sample templates and a JSON data file.
Also See
This post references the following in the documentation:
- TXText
Control. Document Server. Mail Merge Class - TXText
Control. Table Of Contents Class - TXText
Control. Table Of Contents. Update Method
Related Posts
An Ultimate Guide to Mail Merge with MS Word Documents in C#
The MailMerge class provides very effective ways to merge data into MS Word compatible templates. This updated ultimate guide provides an overview of all the important features and functionalities…
Mail Merge with MS Word Documents in C# - An Ultimate Guide
The MailMerge class provides a very effective way to merge data into MS Word compatible templates. This ultimate guide gives an overview of all important features and functionalities of the mail…
Merging Form Fields using the MailMerge Class
Usually, the MailMerge class is used to merge data into document templates to replace merge fields. Since version 30.0, it is possible to merge data into form fields.
MailMerge: Rendering Conditional Table Rows
The MailMerge class supports repeating merge blocks that are repeated based on the given data rows. Sub-blocks can be rendered conditionally based on value comparisons of the parent data table.…
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.