Example - Merging Templates

The most typical application for the HTML5 Web editor is creating templates for the Text Control Reporting engine MailMerge. Using TX Text Control .NET Server for ASP.NET, you can easily deploy a cross-browser, cross-platform template editor with an MS Word look and feel.

This example is part of 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: HTML5\template.aspx

Example description

Simply click the button Load template, add some text or fields and start the merge process by clicking the Merge template server-side button.

On server-side, the sample database XML file is loaded into a new DataSet instance. This DataSet is used as the data source for the MailMerge component. The template is saved into a byte array using the SaveText method and loaded into the MailMerge instance using the LoadTemplateFromMemory method. Finally, the Merge method is called with the created DataSet. The resulting document is saved to memory and from there loaded back into the editor using LoadText.

[C#]
protected void Button1_Click(object sender, EventArgs e)
{
	// load the data source (sample database)
	DataSet ds = new DataSet();
	ds.ReadXml(Server.MapPath("/datasource/sample_db.xml"), XmlReadMode.Auto);

	// save the template
	byte[] data;
	TextControl1.SaveText(out data, 
		TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);

	// load the template into the MailMerge reporting engine
	mailMerge1.LoadTemplateFromMemory(data, 
		TXTextControl.DocumentServer.FileFormat.InternalUnicodeFormat);

	// merge the template
	mailMerge1.Merge(ds.Tables["Sales_SalesOrderHeader"]);

	// save the resulting document to a byte array
	mailMerge1.SaveDocumentToMemory(out data, 
		TXTextControl.BinaryStreamType.InternalUnicodeFormat, null);

	// load the document back into the Web editor
	TextControl1.LoadText(data, 
		TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);
}
[Visual Basic]
Protected Sub Button1_Click(sender As Object, e As EventArgs)
	' load the data source (sample database)
	Dim ds As New DataSet()
	ds.ReadXml(Server.MapPath("/datasource/sample_db.xml"), XmlReadMode.Auto)

	' save the template
	Dim data As Byte()
	TextControl1.SaveText(data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat)

	' load the template into the MailMerge reporting engine
	mailMerge1.LoadTemplateFromMemory(data, TXTextControl.DocumentServer.FileFormat.InternalUnicodeFormat)

	' merge the template
	mailMerge1.Merge(ds.Tables("Sales_SalesOrderHeader"))

	' save the resulting document to a byte array
	mailMerge1.SaveDocumentToMemory(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat, Nothing)

	' load the document back into the Web editor
	TextControl1.LoadText(data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat)
End Sub