MailMerge: MergeFields with CRLFs
Merge fields are pre-formatted in a template and populated with database content during the merge process. Usually, a merge field accepts plain text from a database column field or an XML element value that is formatted with the applied template format. In some applications, it could be helpful to add a carriage return line feed character to merge fields. This sample shows how to use the FieldMerged event to handle this situation. This project shows how to insert a carriage return line feed…

Merge fields are pre-formatted in a template and populated with database content during the merge process. Usually, a merge field accepts plain text from a database column field or an XML element value that is formatted with the applied template format.
In some applications, it could be helpful to add a carriage return line feed character to merge fields. This sample shows how to use the FieldMerged event to handle this situation.
This project shows how to insert a carriage return line feed in the following situations:
- As part of the merge data
- As part of the merge field
- As part of the True and False text in IF fields
The following screenshot shows the demo project. The XML data source is displayed in the panel on the right.

In the first field, the CRLF character ("\r\n") is part of the field data (see right panel in the above screenshot). The second field has "\r\n" defined as the TextAfter property and the IF field contains "\r\n" in the TrueText and FalseText properties. These fields need to be handled in the FieldMerged event:
private void mailMerge1_FieldMerged(object sender,
TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs e)
{
ApplicationField field = e.MailMergeFieldAdapter.ApplicationField;
field.Text = field.Text.Replace(@"\r\n", "\r\n");
}
The string "\r\n" is simply replaced with the CRLF control character.
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 Server (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
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.
Automatically Reconnect to the Server and Recover the Document
We just published a sample project that shows how to reconnect to the server and how to recover the current document.
JavaScript API: Working with Merge Fields
This article gives an overview of how to add, remove and manipulate merge fields programmatically using the JavaScript API.
Technology Preview: Embeddable HTML Widget to integrate Document Editing to…
This technology preview shows an early version of an HTML widget that can be embedded into any HTML page.
Embedding TXTextControl.Web in non-.NET Framework applications like .NET…
This article shows how to embed the ASP.NET MVC component TXTextControl.Web into non-.NET Framework applications.