Available for: UI for ASP.NET AJAX | UI for ASP.NET MVC | UI for WPF | UI for WinForms

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

RadFlowDocument document = CreateDocument(); 
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
editor.InsertField("MERGEFIELD FirstName", ""); 

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

FieldInfo field = new FieldInfo(document); 
 
paragraph.Inlines.Add(field.Start); 
paragraph.Inlines.AddRun("MERGEFIELD LastName"); 
paragraph.Inlines.Add(field.Separator); 
paragraph.Inlines.AddRun(""); 
paragraph.Inlines.Add(field.End); 

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

List<MailMergeRecord> mailMergeDataSource = new List<MailMergeRecord>() 
{ 
    new MailMergeRecord() 
    { 
        FirstName = "Andrew", 
        LastName = "Fuller" 
    }, 
    new MailMergeRecord() 
    { 
        FirstName = "Nancy", 
        LastName = "Davolio" 
    }, 
}; 

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

RadFlowDocument mailMergeResult = document.MailMerge(mailMergeDataSource); 

See Also

In this article
Not finding the help you need? Improve this article