Getting Started

This getting started tutorial shows how to use the Template Designer TX Text Control Words with a sample template and database and illustrates how to integrate the merging process into your ASP.NET Web application.

The source code for this simple example is located in the following directories:

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

Used TX Text Control classes:

Create your application

1. Open Visual Studio and create a new ASP.NET Empty Web Application application.

2. In the Solution Explorer, select the project and choose Add New Item... from the Project main menu.

In the opened dialog Add New Item, select Web Form and confirm with Add.

3. In the Solution Explorer, select the newly created Web Form and choose Component Designer from the View main menu.

Find the MailMerge component in the Toolbox and drag and drop an instance onto the Component Designer form.

Repeat this step with the ServerTextControl component.

4. Select the MailMerge component and set the TextComponent property to serverTextControl1 - the name of the inserted instance of ServerTextControl.

5. While the project is selected in the Solution Explorer, choose Add Existing Item... from the Project main menu.

In the opened dialog Add Existing Item, browse to the following sample folder:

%USERPROFILE%\Documents\TX Text Control 24.0.NET Server for ASP.NET\Samples\ASP.NET\CSharp\Documentation Tutorials\MailMerge\Tutorial\

or

%USERPROFILE%\Documents\TX Text Control 24.0.NET Server for ASP.NET\Samples\ASP.NET\VB.NET\Documentation Tutorials\MailMerge\Tutorial\

Select the following 2 files: template.docx and sample_db.xml and click the Add button.

6. Select the Web Form in the Solution Explorer and choose Designer from the View main menu.

Find the Button control in the Toolbox and drag and drop an instance onto the Web Form.

7. Double-click the button and insert the following code to the event handler:

[C#]
protected void Button1_Click(object sender, EventArgs e)
{
	// create a DataSet from the sample XML data source
	System.Data.DataSet ds = new System.Data.DataSet();
	ds.ReadXml(Server.MapPath("sample_db.xml"), System.Data.XmlReadMode.Auto);

	// load the template
	mailMerge1.LoadTemplate(Server.MapPath("template.docx"), 
		TXTextControl.DocumentServer.FileFormat.WordprocessingML);

	// merge the template with data
	mailMerge1.Merge(ds.Tables[0]);

	// save the document as PDF into a byte array
	byte[] data;
	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();
}
[Visual Basic]
Protected Sub Button1_Click(sender As Object, e As EventArgs)
	' create a DataSet from the sample XML data source
	Dim ds As New System.Data.DataSet()
	ds.ReadXml(Server.MapPath("sample_db.xml"), System.Data.XmlReadMode.Auto)

	' load the template
	mailMerge1.LoadTemplate(Server.MapPath("template.docx"), TXTextControl.DocumentServer.FileFormat.WordprocessingML)

	' merge the template with data
	mailMerge1.Merge(ds.Tables(0))

	' save the document as PDF into a byte array
	Dim data As Byte()
	mailMerge1.SaveDocumentToMemory(data, TXTextControl.BinaryStreamType.AdobePDF, Nothing)

	' 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]()
End Sub

8. In the same code view, add the following code to the Page_Load event:

[C#]
protected void Page_Load(object sender, EventArgs e)
{
	InitializeComponent();
}
[Visual Basic]
Protected Sub Page_Load(sender As Object, e As EventArgs)
	InitializeComponent()
End Sub

Compile and start the application. Press the button to create the report that is returned as a PDF file.