Consider an address block with several merge fields in a mail merge letter. Some fields might be empty such as the firstname and name. If no data is available for those fields, the complete line should be removed. The address block is included in a TextFrame, so that following elements are not moved when an empty line is removed.

Mail Merge: Suppress lines with empty merge fields

Thanks to the flexible API of TX Text Control Reporting, it is very easy to realize such requirements.

MailMerge has the property RemoveEmptyFields that specifies whether empty fields should be removed from the template or not. By default, this property is true, but this removes only the text and not the complete line.

In order to handle the removal programmatically, this property should be set to false to find these fields in the template after it has been merged.

The following code loops through all empty fields and checks whether the field is at the end of a line and the only empty field in that line. If that case is true, the field and the carriage return is removed.

private void RemoveEmptyFieldLines()
{
        foreach (IFormattedText obj in serverTextControl1.TextParts)
        {
                foreach (ApplicationField field in obj.ApplicationFields)
                {
                        // check, if character next to empty field is a carriage return
                        if (obj.TextChars[field.Start + field.Length].Char == '\r')
                        {
                                bool bCompleteLine =
                                        (field.Start == obj.Lines.GetItem(field.Start).Start)
                                        ? true : false;

                                // if yes, remove the field
                                obj.Selection.Start = field.Start;
                                obj.ApplicationFields.Remove(field, false);

                                // if the field is the only text in the line
                                // remove the CR
                                if (bCompleteLine)
                                {
                                        obj.Selection.Length = 1;
                                        obj.Selection.Text = "";
                                }

                                // call RemoveEmptyFieldLines recursively
                                RemoveEmptyFieldLines();
                        }
                        else
                                field.Text = "";
                }
        }
}

This method RemoveEmptyFieldLines is simply called after the MergeObjects method of MailMerge:

mailMerge1.MergeObjects(reports);
// remove empty lines
RemoveEmptyFieldLines();
Mail Merge: Suppress lines with empty merge fields

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

GitHub

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)