MailMerge: MergeFields with CRLFs
TX Text Control MailMerge ignores carriage return and line feed characters in merge field data by default. The FieldMerged event provides a way to detect CRLF sequences in field values, merge data, and IF field conditions, then replace them with actual text breaks in output.

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
The MailMerge and ServerTextControl components of TX Text Control .NET Server for ASP.NET enable server-side reporting in Web Forms. A template.docx merges with XML data via a button click…
Automatically Reconnect to the Server and Recover the Document
When a WebSocket connection drops, the TX Text Control editor automatically reconnects and recovers the document using browser local storage. The implementation saves document state client-side…
JavaScript API: Working with Merge Fields
The TX Text Control JavaScript API enables inserting, deleting, and manipulating merge fields in the HTML5-based document editor. Developers create MergeField objects, add them at specific…
Technology Preview: Embeddable HTML Widget to integrate Document Editing to…
TX Text Control offers a technology preview of an embeddable JavaScript widget that adds document editing to Angular, React, and other web frameworks. Developers add a script tag and create a…
Embedding TXTextControl.Web in non-.NET Framework applications like .NET…
The HTML5-based Web.TextControl editor can run inside an IFrame to embed it in non-.NET Framework applications such as .NET Core or AngularJS sites. The hosting page sends structured JSON commands…
