Mail Merging via Document Server

This demo illustrates a scenario, in which mail merge operations are performed dynamically with the help of an external RichEditDocumentServer component.

The RichEditDocumentServer loads a predefined template document containing the static content with embedded mail merge data fields, binds to an external data source and generates the merged document. In this demo, the Bootstrap Rich Text Editor is used to open the resulting merged document for display within a web page.

<dx:BootstrapRichEdit ID="RichEditMailMergeViaDocumentServer" runat="server" ReadOnly="true" RibbonMode="None" ShowStatusBar="false"
    Settings-HorizontalRuler-Visibility="Hidden" ShowConfirmOnLosingChanges="false" OnLoad="RichEditMailMergeViaDocumentServer_Load">
</dx:BootstrapRichEdit>
string documentId = "mailMergeDocId";
protected void RichEditMailMergeViaDocumentServer_Load(object sender, EventArgs e) {
    if(!IsPostBack) {
        RichEditDocumentServer documentServer = new RichEditDocumentServer();
        documentServer.LoadDocument(Path.Combine(DirectoryManagmentUtils.CurrentDataDirectory, @"RichEdit\\MailMergeTemplate.docx"));
        documentServer.Options.MailMerge.DataSource = GetData();
        using(MemoryStream stream = new MemoryStream()) {
            documentServer.MailMerge(stream, DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
            stream.Position = 0;
            DevExpress.Web.Office.DocumentManager.CloseDocument(documentId);
            RichEditMailMergeViaDocumentServer.Open(documentId, DevExpress.XtraRichEdit.DocumentFormat.OpenXml, () => {
                return stream;
            });
        }
    }
}
List<Employee> GetData() {
    return new NorthwindContextSL().Employees.ToList();
}
Screen Size
Color Themes
Demo QR Code