Products Technologies Demo Docs Blog Support Company

Reporting: Custom Processing Using MailMerge Events

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…

Reporting: Custom Processing Using MailMerge Events

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.

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.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

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

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

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

Reporting

The Text Control Reporting Framework combines powerful reporting features with an easy-to-use, MS Word compatible word processor. Users can create documents and templates using ordinary Microsoft Word skills. The Reporting Framework is included in all .NET based TX Text Control products including ASP.NET, Windows Forms and WPF.

See Reporting products

Related Posts

ASP.NETReportingHTML5

Creating Your First ASP.NET Reporting Application

This tutorial shows how to use the MailMerge component in an ASP.NET Web application to merge a template with data to create an Adobe PDF document.


ASP.NETReportingTutorial

New Online Sample: Build your First Report

We published a new online demo that shows how to create a report including preparing data, creating a template to merging them together.


ReportingDocumentationReportingCloud

Create your First Document with ReportingCloud

As part of our new ReportingCloud documentation, we published a guided tutorial that shows how to create a document without programming.


CloudReportingMail Merge

MailMerge: Starting Each Merge Block on a New Page

A merge block is repeated based on the number of matching data rows in the hierarchical data object. The complete merge block is cloned and inserted under the original location in the template.…


ReportingTutorialWeb API

Using MailMerge with JSON Data

In the last article, we explained how to create an ASP.NET Web API to merge templates with JSON data in the payload body of an HTTP request. The focus of this article was on the Web API and…