Products Technologies Demo Docs Blog Support Company

MailMerge: Merge CheckBoxes During the Merge Process

TX Text Control MailMerge processes standard MergeFields automatically, but form checkboxes require additional handling. By iterating ApplicationFields in the DataRowMerged event and using the FormCheckBox adapter, developers can set checkbox state from boolean values in the data source.

MailMerge: Merge CheckBoxes During the Merge Process

TX Text Control's reporting engine MailMerge supports commonly used fields compatible with the mail merge functionality of MS Word including MergeFields, IncludeText fields and Date fields. Besides those standard fields, the DocumentServer namespace contains field adapter for MS Word form fields, such as checkboxes, drop-down boxes and form text fields, and content control fields.

MergeFields that are merged automatically during the MailMerge merge process can be also combined with form fields such as checkboxes. The following method loops through the ApplicationFields in order to merge included check boxes:

private byte[] processFormCheckBoxFields(byte[] document, DataRow dataRow)
{
    // load merged row into a temporary ServerTextControl
    using (TXTextControl.ServerTextControl tx =
        new TXTextControl.ServerTextControl())
    {
        tx.Create();
        tx.Load(document, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

        // loop through all text parts
        foreach (IFormattedText textPart in tx.TextParts)
        {
            // loop through all form checkbox fields
            foreach (ApplicationField appField in textPart.ApplicationFields)
            {
                if (appField.TypeName == "FORMCHECKBOX")
                {
                    FormCheckBox checkbox = new FormCheckBox(appField);

                    // set the value
                    if(dataRow.Table.Columns[checkbox.Name] != null)
                        checkbox.Checked =
                            Convert.ToBoolean(dataRow[checkbox.Name]);
                }
            }
        }

        byte[] data;
        tx.Save(out data, BinaryStreamType.InternalUnicodeFormat);
        return data;
    }
}

The MailMerge class merges only merge fields as part of the merge process. After the merging process, included form fields are still part of the document. In the DataRowMerged event, the merged document is simply passed to the processFormCheckBoxFields method to process the form fields. The resulting document is returned in the MergedRow property:

private void mailMerge1_DataRowMerged(object sender,
                TXTextControl.DocumentServer.MailMerge.DataRowMergedEventArgs e)
{
    // return the modified merged row document
    e.MergedRow = processFormCheckBoxFields(e.MergedRow,
        ds.Tables[0].Rows[e.DataRowNumber]);
}

The value for the check boxes are simple boolean fields in the data source:

<report>
  <entry>
    <name>Petersen</name>
    <firstname>Paul</firstname>
    <check1>true</check1>
    <check2>false</check2>
  </entry>
  <entry>
    <name>Morrison</name>
    <firstname>Jamie</firstname>
    <check1>true</check1>
    <check2>true</check2>
  </entry>
  <entry>
    <name>Woldorf</name>
    <firstname>Walter</firstname>
    <check1>false</check1>
    <check2>false</check2>
  </entry>
  <entry>
    <name>Jackson</name>
    <firstname>Peter</firstname>
    <check1>true</check1>
    <check2>true</check2>
  </entry>
</report>

The following screenshot shows the resulting document of the sample project:

MailMerge: Merge CheckBoxes during the merge process

You can download the sample project for your own tests. At least a trial version of TX Text Control .NET for Windows Forms is required.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

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

Creating Your First ASP.NET Reporting Application

The MailMerge and ServerTextControl components of TX Text Control .NET Server for ASP.NET enable server-side reporting in Web Forms. A template.docx merges with XML data via a button click…


ASP.NETReportingTutorial

New Online Sample: Build your First Report

A new interactive online demo walks through building a report with TX Text Control in three steps: preparing JSON data in a live editor, creating a template with merge fields and repeating blocks,…


ReportingDocumentationReportingCloud

Create your First Document with ReportingCloud

ReportingCloud documentation now includes interactive tutorials for creating documents without code. Users enter an API key, choose a format such as PDF or DOCX, customize the merge data payload,…


CloudReportingMail Merge

MailMerge: Starting Each Merge Block on a New Page

Merge blocks in TX Text Control repeat based on matching data rows. Applying ParagraphFormat.PageBreakBefore to the first paragraph of a block forces each repetition onto a new page. Section…


ReportingTutorialWeb API

Using MailMerge with JSON Data

Merge document templates with JSON data using TX Text Control MailMerge by converting nested JSON strings into DataSet objects via Newtonsoft.Json. The JSON is first transformed to XML, then…

Share on this blog post on: