Products Technologies Demo Docs Blog Support Company

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.

Combining MailMerge and Table of Contents

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.

TX Text Control and TOC

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.

TX Text Control and TOC

Update TOC

After the document has been merged using the MailMerge class, the TableOfContents objects must be updated using the Update method.

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:

TX Text Control and TOC

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.

sample_templates.zip

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.DocumentServer.MailMerge Class
  • TXTextControl.TableOfContents Class
  • TXTextControl.TableOfContents.Update Method

Related Posts

ASP.NETWindows FormsWPF

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…


ASP.NETWindows FormsWPF

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…


ASP.NETWindows FormsWPF

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.


ASP.NETWindows FormsWPF

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.…


ASP.NETWindows FormsWPF

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.