TX Text Control Blog

Reporting: Custom processing using MailMerge events

Björn Meyer

Blogged by Björn Meyer on May 7, 2014 and tagged with reporting, tutorial.


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.

Download and fork this sample on GitHub

We proudly host our sample code on github.com/textcontrol. Feel free to fork and contribute!

Download ZIP

Open repository on GitHub

Open in Visual Studio

Requirements for this sample

  • at least Visual Studio 2012
  • At least a trial version of TX Text Control .NET for Windows Forms X13 (23.0)

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.

 
 
Call us: +1 704-544-7445 (United States)
 
 

Products

Support

Downloads

Corporate

Buy Now

About Text Control

Founded in 1991, Text Control is an award-winning Visual Studio Industry Partner and leading vendor of word processing and reporting components for Microsoft development technologies. The products help thousands of developers add comprehensive word processing functionality to their applications. Text Control is 100% self-owned and independent of all investor decisions. Customers benefit from these years of experience, large user-base, and at the same time, appreciate developing with a mature, reliable product.

SD Times 100SD Times 100SD Times 100SD Times 100SD Times 100SD Times 100Visual Studio 2015 Launch PartnerVisual Studio 2013 Launch PartnerVisual Studio 2012 Launch PartnerVisual Studio Partner