Create Your Application
-
Open Visual Studio and create a new ASP.NET Empty Web Application.
-
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.
-
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.
-
Select the MailMerge component and set the TextComponent property to serverTextControl1 - the name of the inserted instance of ServerTextControl.
-
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 29.0.NET Server for ASP.NET\Samples\ASP.NET\CSharp\Documentation Tutorials\MailMerge\Tutorial
or
%USERPROFILE%\Documents\TX Text Control 29.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.
-
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.
-
Double-click the button and insert the following code to the event handler:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersprotected 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(); } This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersProtected 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 -
In the same code view, add the following code to the Page_Load event:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersprotected void Page_Load(object sender, EventArgs e) { InitializeComponent(); } This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersProtected 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.