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.

MailMerge: MergeFields with CRLFs

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");
}
view raw Form1.cs hosted with ❤ by GitHub

The string "\r\n" is simply replaced with the CRLF control character.

Download the sample from GitHub and test it on your own.