MailMerge: Conditional INCLUDETEXT Fields
In documents and reports, often sentences or an appendix are added only under specific conditions. The Text Control reporting engine MailMerge provides the concept of INCLUDETEXT fields to include separate, formatted documents at a specific input position in the template. The included documents can be loaded from a physical file in various supported formats such as DOCX, DOC and RTF or formatted text can be injected by handling the IncludeTextMerging event. The insertion can be also canceled…

In documents and reports, often sentences or an appendix are added only under specific conditions. The Text Control reporting engine MailMerge provides the concept of INCLUDETEXT fields to include separate, formatted documents at a specific input position in the template. The included documents can be loaded from a physical file in various supported formats such as DOCX, DOC and RTF or formatted text can be injected by handling the IncludeTextMerging event.
The insertion can be also canceled in the same event by returning a blank document. This article shows a best practice how to include sub-templates under specific conditions controlled by a field in the data source.
The Template
In the template, all possible INCLUDETEXT fields are added at the desired locations. In this sample, these fields are inserted adjacent to each other to avoid empty lines in the final document when an INCLUDETEXT field is conditionally removed.

The INCLUDETEXT fields get the filename of the sub-templates: phrase1.docx and phrase2.docx.
The Data Source
This sample uses an XML file as the data source with 3 data rows. Each data row contains a field for the included sub-templates (in this case: phrase1.docx and phrase2.docx). As a convention, the value 1 means that the sub-template should be added and 0 means, it should be omitted.
<journal>
<report>
<name>Alpha Beta Group, LLC</name>
<phrase1.docx>1</phrase1.docx>
<phrase2.docx>1</phrase2.docx>
</report>
<report>
<name>Gamma Consulting Corporation</name>
<phrase1.docx>0</phrase1.docx>
<phrase2.docx>1</phrase2.docx>
</report>
<report>
<name>Delta Fitness Company</name>
<phrase1.docx>1</phrase1.docx>
<phrase2.docx>0</phrase2.docx>
</report>
</journal>
Event Handling
To control the conditional sub-templates, 2 events must be handled: DataRowMerged to count the currently merged data row and IncludeTextMerging to check whether the sub-template should be inserted or not.
int iCurrentDataRow = 0;
private void mailMerge1_DataRowMerged(object sender,
TXTextControl.DocumentServer.MailMerge.DataRowMergedEventArgs e)
{
iCurrentDataRow = e.DataRowNumber + 1;
}
private void mailMerge1_IncludeTextMerging(object sender,
TXTextControl.DocumentServer.MailMerge.IncludeTextMergingEventArgs e)
{
byte[] data;
// create blank document as byte array
using(TXTextControl.ServerTextControl tx =
new TXTextControl.ServerTextControl())
{
tx.Create();
tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
}
// remove the include text field in case the data contains a "0"
if (ds.Tables[0].Rows[iCurrentDataRow][e.Filename].ToString() == "0")
e.IncludeTextDocument = data;
}
In the IncludeTextMerging event, the data field that matches the name of the INCLUDETEXT field is verified to include or omit the insertion. The code in the events is generalized as it doesn't contain any hard coded field names and the data source itself is used to control the insertion.
The results of the above sample are 3 documents where both sub-templates are included in the first document, the first is omitted in the second document and only the first phrase is inserted in the third document:

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.
Please fork and contribute.
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.
Related Posts
Windows Forms: Printing Multiple Pages Per Sheet
This sample project implements the class MultipagePrintDocument that inherits from System.Drawing.Printing.PrintDocument to print multiple pages of a document per sheet. The constructor of…
Inserting Watermark Images to All Pages Dynamically
This sample project shows how to create and insert a watermark image on all pages dynamically. Image objects have the Name property to store additional string information with an image. This…
Merging Documents with RESTful Web API's
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for…
ReportingWindows FormsTutorial
Reporting: Sorting Merge Block Rows by Column Name
The concept of Text Control Reporting is to accept pre-shaped data. That means that Text Control's MailMerge component is merging the data rows 'as-is' and data shaping, sorting or queries are…
Reporting: Merging MS Word Documents with DocVariables
TX Text Control supports all MS Word fields including the typical merge fields, content control fields, legacy form fields or special fields such as DOCVARIABLE. All of those fields can be found…