ASP.NET Reporting/MailMerge Tutorial

Back to tutorial overview

Reporting Tutorial - Create your first ASP.NET reporting application

This tutorial shows how to use the MailMerge component in an ASP.NET Web application to merge a template with data to create an Adobe PDF document.

Create your application

  1. Open Visual Studio and create a new ASP.NET Empty Web 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
  9. Compile and start the application. Press the button to create the report that is returned as a PDF file.

 
 
Call us: +1 704-544-7445 (United States)
 
 

Products

Support

Downloads

Corporate

Buy Now

About Text Control

Founded in 1991, Text Control is an award-winning Visual Studio Industry Partner and leading vendor of word processing and reporting components for Microsoft development technologies. The products help thousands of developers add comprehensive word processing functionality to their applications. Text Control is 100% self-owned and independent of all investor decisions. Customers benefit from these years of experience, large user-base, and at the same time, appreciate developing with a mature, reliable product.

SD Times 100SD Times 100SD Times 100SD Times 100SD Times 100SD Times 100Visual Studio 2015 Launch PartnerVisual Studio 2013 Launch PartnerVisual Studio 2012 Launch PartnerVisual Studio Partner