Text Control's Reporting engine MailMerge is based on TX Text Control .NET and encapsulates the required mail merge features such as looping through merge fields, repeating blocks and populating fields with data. It allows you to create professional reports without writing a single line of code.

The component MailMerge is connected to an instance of TX Text Control that is visible (TXTextControl.TextControl) or not (TXTextControl.ServerTextControl).

Learn more about reporting and how to create your first Text Control Reporting application here:

Text Control Reporting

A huge advantage is the flexibility you gain when using Text Control Reporting compared to banded reporting tools. Thanks to the complete and powerful API, you can post-process the created documents programmatically or modify them in the editor TX Text Control manually.

During the merge process, there are 3 events that can be used to manipulate the document with the TX Text Control API:

  1. FieldMerged
    This event is raised for each merged field and can be used to inject formatted text into merge fields.

  2. DataRowMerged
    This event is raised for each completed data row of the most outer data source object or table can be used to manipulate this complete record.

  3. BlockRowMerged
    This event is raised for each completed row of a merge block and can be used to manipulate this complete row.

All of these events return the complete content as a byte array in the internal TX Text Control format. This can be a single word or character, a table row or a complete document. This byte array can be loaded into a non-UI ServerTextControl, post-processed and loaded back into the merge document.

The following sample merges a template with an address table. For each data row, the contained tables are resized to apply AutoFit settings to a table.

Custom processing using MailMerge events
private void mailMerge1_DataRowMerged(object sender,
        TXTextControl.DocumentServer.MailMerge.DataRowMergedEventArgs e)
{
        using (TXTextControl.ServerTextControl tx =
                new TXTextControl.ServerTextControl())
        {
                tx.Create();

                tx.Load(e.MergedRow,
                        TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                foreach (Table table in tx.Tables)
                {
                        table.AutoSize(tx);
                }

                byte[] data;

                tx.Save(out data, BinaryStreamType.InternalUnicodeFormat);
                e.MergedRow = data;
        }
}

After the merge, the table on each page has been adjusted to fit the cell content:

Custom processing using MailMerge events

Download the sample from GitHub and test it on your own.

GitHub

Download and Fork This Sample on GitHub

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

Please fork and contribute.

Requirements for This Sample

  • Visual Studio 2012 or better
  • TX Text Control .NET for Windows Forms (trial sufficient)

Summary: Text Control Reporting gives you a very powerful way to merge MS Word compatible templates automatically, but at the same time a flexible API to modify the merged document, if necessary.