Create Your Application

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

    image

  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.

    image

  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.

    image

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

    image

  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 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.

    image

  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.

    image

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

    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();
    }
    view raw gist.cs hosted with ❤ by GitHub
    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
    view raw gist.vb hosted with ❤ by GitHub
  8. In the same code view, add the following code to the Page_Load event:

    protected void Page_Load(object sender, EventArgs e)
    {
    InitializeComponent();
    }
    view raw gist.cs hosted with ❤ by GitHub
    Protected Sub Page_Load(sender As Object, e As EventArgs)
    InitializeComponent()
    End Sub
    view raw gist.vb hosted with ❤ by GitHub
  9. Compile and start the application. Press the button to create the report that is returned as a PDF file.