Products Technologies Demo Docs Blog Support Company

MailMerge: The Append Parameter of the Merge Method Explained

The MailMerge Merge method accepts an append parameter that controls document output. When true, all data rows merge into one document. When false, each row produces a separate document accessible through the DataRowMerged event, where MergedRow returns the internal format.

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

The MailMerge class provides four properties to control merge output: RemoveEmptyFields, RemoveEmptyLines, RemoveEmptyBlocks, and RemoveEmptyImages. Each property handles unmatched or missing data…


ReportingWindows FormsExcel

Merge Excel Documents into MailMerge Templates using IncludeText Fields

IncludeText fields in TX Text Control templates reference external Excel files during mail merge. The process loads .xlsx files as temporary ServerTextControl instances, using Bookmark to target…


ASP.NETReportingWindows Forms

MailMerge: Conditional Table Cell Colors using Filter Instructions

The TX Text Control MailMerge FieldMerged event exposes TableCell instances during merge for conditional formatting. A CellFilterInstructions class parses filter rules from merge field names and…


ASP.NETReportingWindows Forms

MailMerge: Using Filters to Remove Unwanted Rows

TX Text Control merge block filters remove unwanted rows from repeating data during the mail merge process without modifying the underlying source. Filter conditions applied to merge blocks…


ReportingWindows FormsMail Merge

MailMerge: Conditional Rendering of Merge Blocks

TX Text Control X16 provides two approaches to conditionally render merge blocks within MailMerge templates. Setting RemoveEmptyBlocks removes blocks when child data is absent, while…

Share on this blog post on: