Example - Creating a Simple Mail-Merge Letter Report

The class MailMerge encapsulates typical reporting tasks and can be connected to an instance of ServerTextControl. A template is simply loaded and data is passed from a DataSet, DataTable or an IEnumerable object. All merge fields and merge blocks are populated automatically using existing data relations.

This example is part the Sample Explorer project which includes all samples in one. The sources for this sample project can be found here:

Language Project Location
C# %USERPROFILE%\Documents\TX Text Control 24.0.NET Server for ASP.NET\Samples\ASP.NET\CSharp\Sample Explorer\
VB.NET %USERPROFILE%\Documents\TX Text Control 24.0.NET Server for ASP.NET\Samples\ASP.NET\VB.NET\Sample Explorer\

You will find the source code and the ASPX page of this sample in the Solution structure tree under: Reporting\create.aspx

Example description

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.

This business object is created based on data that is completed using HTML form fields. Change the pre-populated field values and click Create Report to start the merge process.

A template is loaded into an instance of MailMerge. The created business object is used as the data source in the MergeObjects method. The created document is finally loaded into the UI component DocumentViewer.

[C#]
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;
}
[Visual Basic]
Protected Sub btnCreateReport_Click(sender As Object, e As EventArgs)
	' 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(data, _ 
		TXTextControl.BinaryStreamType.InternalUnicodeFormat, Nothing)
	DocumentViewer1.LoadDocumentFromMemory(data, _
		TXTextControl.DocumentServer.FileFormat.InternalUnicodeFormat)

	btn_DownloadReport.Visible = True
End Sub