Mail Merge
Mail merge is functionality allowing to produce personalized documents from a template holding fixed content and variables. The variables are called Merge Fields and are replaced through the merge process with content from a specified data source.
Inserting Merge Fields
Merge fields are a type of Fields and can be added in a template document via RadFlowDocumentEditor's InsertField() method. The method requires the code representation of the field and the result which is shown in the template before the document is mail-merged.
The code snippet in Example 1 shows how to initialize a RadFlodDocumentEditor instance and insert a merge field.
Example 1: Insert a merge field
Additionally, a field can be added to a Paragraph manually by creating a FieldInfo instance and placing its start, code, separator, result and end in the block. Example 2 shows the manual approach for adding a merge field.
Example 2: Add a merge field manually
Performing Mail Merge
Mail merge can be performed over a template document containing merge fields. For this action the MailMerge() method of RadFlowDocument needs to be used. The method accepts a collection of elements as a parameter.
During the operation, each MergeField is replaced with the corresponding information from the data source record in a new RadFlowDocument instance. Every subsequent entry in the data source is appended to a single resulting document which is returned by the method. The original template stays unmodified.
Example 3 shows a simple example data source.
Example 3: Sample data source
Example 4 performs the mail merge operation over a previously defined template document using the data source from Example 3.
Example 4: Perform mail merge
Nested Mail Merge
The nested mail merge functionality is supported from R1 2022. It allows you to merge data sources that contain nested data. For example, your business object can contain a list of other objects and this functionality allows accessing the properties of the underlying objects. In order to use the underlying objects, you need to declare a group. Currently, the following group tags are supported:
- BeginGroup/EndGroup
- TableStart/TableEnd
- RangeStart/RangeEnd
- GroupStart/GroupEnd
Currently, all tag pairs work equally and more than one option exists in order to improve the readability of the documents.
Exception: When a table row has only one cell, using the TableStart/TableEnd tags over the whole content of that cell will create a new row for each value. Every other pair of tags (BeginGroup/EndGroup, RangeStart/RangeEnd, GroupStart/GroupEnd) are interchangeable and will put the values on the same row inside that cell.
A single cell (spanning the whole row) with TableStart/TableEnd tags:
A single cell (spanning the whole row) with a tag group different than TableStart/TableEnd:
The following example demonstrates how you can use the nested mail merge:
First you need to define a data source that contains an IEnumerable
of objects.
Example 5: Nested mail merge data source
Example 6: Perform nested mail merge
One Row vs Multiline Mail Merge
With the nested mail merge functionality, it is possible to add all items to a single line. This is achieved by adding the group and regular fields to a single paragraph.
Figure 1: Mail Merging on a single row and the results
If you want to separate the items into several rows you need to close the group on the next row
Figure 2: Mail Merging on multiple rows row and the results