RadFlowDocument document =
new
RadFlowDocument();
RadFlowDocumentEditor editor =
new
RadFlowDocumentEditor(document);
editor.InsertText(
"Hello "
);
editor.InsertField(
"MERGEFIELD FirstName "
,
"«FirstName»"
);
editor.InsertText(
" "
);
editor.InsertField(
"MERGEFIELD LastName "
,
"«LastName»"
);
editor.InsertText(
","
);
editor.InsertParagraph();
editor.InsertParagraph();
editor.InsertText(
"On behalf of "
);
editor.InsertField(
"MERGEFIELD CompanyName "
,
"«CompanyName»"
);
editor.InsertText(
", "
);
editor.InsertText(
"I would like to thank you for purchasing "
);
editor.InsertField(
"MERGEFIELD PurchasedItemsCount "
,
"«PurchasedItemsCount»"
);
editor.InsertText(
" "
);
editor.InsertField(
"MERGEFIELD ProductName "
,
"«ProductName»"
);
editor.InsertText(
" from us."
);
editor.InsertParagraph();
editor.InsertText(
"We are committed to provide you with the highest level of customer satisfaction possible. "
);
editor.InsertText(
"If for any reasons you have questions or comments please call "
);
editor.InsertField(
"MERGEFIELD ProductSupportPhone "
,
"«ProductSupportPhone»"
);
editor.InsertText(
" "
);
editor.InsertField(
"MERGEFIELD ProductSupportPhoneAvailability "
,
"«ProductSupportPhoneAvailability»"
);
editor.InsertText(
", or email us at "
);
editor.InsertField(
"MERGEFIELD ProductSupportEmail "
,
"«ProductSupportEmail»"
);
editor.InsertText(
"."
);
editor.InsertParagraph();
editor.InsertText(
"Once again thank you for choosing "
);
editor.InsertField(
"MERGEFIELD CompanyName "
,
"«CompanyName»"
);
editor.InsertText(
"."
);
editor.InsertParagraph();
editor.InsertParagraph();
editor.InsertText(
"Sincerely yours,"
);
editor.InsertParagraph();
editor.InsertField(
"MERGEFIELD SalesRepFirstName "
,
"«SalesRepFirstName»"
);
editor.InsertText(
" "
);
editor.InsertField(
"MERGEFIELD SalesRepLastName "
,
"«SalesRepLastName»"
);
editor.InsertText(
","
);
editor.InsertParagraph();
editor.InsertField(
"MERGEFIELD SalesRepTitle "
,
"«SalesRepTitle»"
);
And here is the result saved in DOCX format:
Once we have a template, it’s time to prepare for the data source merging. The only requirement to the data source is to implement the IEnumerable interface. This way, the document template will be able to iterate over all records and produce the final merged document. The records in the source need to contain properties corresponding to all the merge field names. Here is the class definition of the mail-merge record for our example:
public
class
FollowUpData
{
public
string
FirstName {
get
;
set
; }
public
string
LastName {
get
;
set
; }
public
string
CompanyName {
get
;
set
; }
public
int
PurchasedItemsCount {
get
;
set
; }
public
string
ProductName {
get
;
set
; }
public
string
ProductSupportPhone {
get
;
set
; }
public
string
ProductSupportPhoneAvailability {
get
;
set
; }
public
string
ProductSupportEmail {
get
;
set
; }
public
string
SalesRepFirstName {
get
;
set
; }
public
string
SalesRepLastName {
get
;
set
; }
public
string
SalesRepTitle {
get
;
set
; }
}
RadFlowDocument mailMergedDocument = document.MailMerge(mailMergeDataSource);
And that’s all! The mail merge is finished. The following image illustrates the final result of our example with one record:
The used code is available for download in our SDK repository on GitHub.Mihail Vladov is a Software Engineering Manager at Progress. He has more than a decade of experience with software and product development and is passionate about good software design and quality code. Mihail helped develop the WPF controls suite and Document Processing libraries which are used by thousands of developers. Currently, he is leading the JustMock team. In his free time, he loves to travel and taste different foods. You can find Mihail on LinkedIn.