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.
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.
This method RemoveEmptyFieldLines is simply called after the MergeObjects method of MailMerge:
Download the sample from GitHub and test it on your own.