Creating a simple Mail-Merge letter report
The class MailMerge encapsulates typical reporting tasks and can be connected to an instance of ServerTextControl. You simply need to load a template and pass data from a DataSet, DataTable or an IEnumerable object. All merge fields and merge blocks are populated automatically using existing data relations.
In this demo, a MailMerge instance is connected to a ServerTextControl to create a simple report. A Mail-Merge report is a fully featured word processing document with merge fields, image placeholders or barcodes that is merged with data from one or many data sources.
For this demo, the data source, that contains merge field data such as address details, is an IEnumerable object.
Demo
MailMerge can be used with data from a DataSet, DataTable or an IEnumerable object. Nested tables and their DataRelations are recognized automatically.
Sources
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace tx_aspnet_samples.Reporting
{
public partial class simple : System.Web.UI.Page
{
private TXTextControl.DocumentServer.DocumentController documentController1;
private System.ComponentModel.IContainer components;
private TXTextControl.ServerTextControl serverTextControl1;
private TXTextControl.DocumentServer.MailMerge mailMerge1;
private TXTextControl.ServerTextControl serverTextControl2;
// wrapper IEnumerable object as merge data source
public List<Report> Reports = new List<Report>();
byte[] data;
// simple object with public properties that match
// the merge field names in the template
public class Report
{
public string Sender { get; set; }
public string Name { get; set; }
public string Firstname { get; set; }
public string Company { get; set; }
public string AddressLine { get; set; }
public string ZIP { get; set; }
public string City { get; set; }
}
protected void btnCreateReport_Click(object sender, EventArgs e)
{
// build a data object for the merge process
Reports.Add(CreateBusinessObject());
// load the template
mailMerge1.LoadTemplate(Server.MapPath("/documents/template.docx"),
TXTextControl.DocumentServer.FileFormat.WordprocessingML);
// merge the template with the IEnumerable business object
mailMerge1.MergeObjects(Reports, true);
// save the document to memory for preview purposes
// in the DocumentViewer
mailMerge1.SaveDocumentToMemory(out data,
TXTextControl.BinaryStreamType.InternalUnicodeFormat, null);
DocumentViewer1.LoadDocumentFromMemory(data,
TXTextControl.DocumentServer.FileFormat.InternalUnicodeFormat);
btn_DownloadReport.Visible = true;
}
// create a data object with the form field entries
private Report CreateBusinessObject()
{
Report report = new Report();
report.Sender = "Peter Jackson";
report.Name = tbName.Text;
report.Firstname = tbFirstName.Text;
report.Company = tbCompany.Text;
report.AddressLine = tbAddressLine.Text;
report.ZIP = tbZIP.Text;
report.City = tbCity.Text;
return report;
}
protected void btn_DownloadReport_Click(object sender, EventArgs e)
{
// create the data object
Reports.Add(CreateBusinessObject());
// load the template and merge
mailMerge1.LoadTemplate(Server.MapPath("/documents/template.docx"),
TXTextControl.DocumentServer.FileFormat.WordprocessingML);
mailMerge1.MergeObjects(Reports, true);
// save the document to memory as PDF
mailMerge1.SaveDocumentToMemory(out data,
TXTextControl.BinaryStreamType.AdobePDF, null);
// return the document to the browser for download
Response.Clear();
Response.AddHeader("content-disposition",
String.Format("attachment;filename={0}", "created_by_txtextcontrol.pdf"));
Response.ContentType = "application/pdf";
Response.BinaryWrite(data);
Response.End();
}
}
}
Mail Merge - Reporting