Products Technologies Demo Docs Blog Support Company

MailMerge: The Append Parameter of the Merge Method Explained

The Merge method of the MailMerge class merges all records into a single document by default. The boolean parameter append specifies whether all resulting documents should be merged into one resulting document or split into separate documents. When MailMerge is connected to a TextComponent such as the visual TXTextControl.TextControl, the resulting document is loaded back into the connected Text Control. In case the Merge method has been used with the append parameter false, only the last…

MailMerge: The Append Parameter of the Merge Method Explained

The Merge method of the MailMerge class merges all records into a single document by default.

The boolean parameter append specifies whether all resulting documents should be merged into one resulting document or split into separate documents.

public void Merge(System.Data.DataTable mergeData, bool append)

When MailMerge is connected to a TextComponent such as the visual TXTextControl.TextControl, the resulting document is loaded back into the connected Text Control. In case the Merge method has been used with the append parameter false, only the last created document is loaded back into the Text Control.

In order to access each single created document, the DataRowMerged event can be used to save the resulting, single document in this event. The event arguments DataRowMergedEventArgs return the merged document in the internal TX Text Control format that can be accessed using the MergedRow property.

The following sample shows how to use the event by merging the following simple template consisting of two merge fields:

The append parameter of the Merge method explained

As a data source, a simple DataSet with one DataTable is used. Before the Merge method is called with the append parameter set to false, the DataRowMerged event is attached:

DataTable dt = new DataTable(); 
dt.Columns.Add("company"); 
dt.Columns.Add("name"); 
 
dt.Rows.Add(new string[] { "Text Control", "Peter Jackson" }); 
dt.Rows.Add(new string[] { "Microsoft", "Sue Williamson" }); 
dt.Rows.Add(new string[] { "Google", "Peter Gustavsson" }); 
 
mailMerge1.LoadTemplate(Server.MapPath("template.docx"), 
    TXTextControl.DocumentServer.FileFormat.WordprocessingML); 
 
mailMerge1.DataRowMerged += mailMerge1_DataRowMerged; 
mailMerge1.Merge(dt, false);

In the DataRowMerged event, each merged record is loaded into a temporary ServerTextControl and saved as HTML which is then sent back to the browser using HttpResponse:

void mailMerge1_DataRowMerged(object sender, 
    TXTextControl.DocumentServer.MailMerge.DataRowMergedEventArgs e) 
{ 
    string htmlDocument = ""; 
 
    using (TXTextControl.ServerTextControl tx = 
        new TXTextControl.ServerTextControl()) 
    { 
        tx.Create(); 
        tx.Load(e.MergedRow, 
            TXTextControl.BinaryStreamType.InternalUnicodeFormat); 
        tx.Save(out htmlDocument, TXTextControl.StringStreamType.HTMLFormat); 
    } 
 
    Response.Write(htmlDocument); 
    Response.Write("<hr />"); 
}

The resulting HTML shows the 3 merged records divided by an horizontal rule:

The append parameter of the Merge method explained

Download the sample from GitHub and test it on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2015 or better
  • TX Text Control .NET Server (trial sufficient)

Reporting

The Text Control Reporting Framework combines powerful reporting features with an easy-to-use, MS Word compatible word processor. Users can create documents and templates using ordinary Microsoft Word skills. The Reporting Framework is included in all .NET based TX Text Control products including ASP.NET, Windows Forms and WPF.

See Reporting products

Related Posts

ASP.NETReportingMail Merge

MailMerge Class Settings Explained

This article explains the different settings of the MailMerge class and how and when they should be used.


ReportingWindows FormsExcel

Merge Excel Documents into MailMerge Templates using IncludeText Fields

This sample shows how to use IncludeText fields to merge Excel documents into templates using MailMerge.


ASP.NETReportingWindows Forms

MailMerge: Conditional Table Cell Colors using Filter Instructions

The MailMerge class provides an extensible framework to inject custom logic to the merge process. This sample shows how to implement conditional table cell colors.


ASP.NETReportingWindows Forms

MailMerge: Using Filters to Remove Unwanted Rows

Data in merge blocks can be filtered and sorted. This article explains how to use filters to remove unwanted lines.


ReportingWindows FormsMail Merge

MailMerge: Conditional Rendering of Merge Blocks

Merge blocks can be used to render content conditionally. This article explains different ways to control this.