Products Technologies Demo Docs Blog Support Company

Reporting: Conditional Formatted Text Blocks

Conditional text blocks in TX Text Control templates use paired IF fields with StartIF and EndIF markers to show or hide formatted content during mail merge. The DataRowMerged event removes content between markers when conditions are met, supporting tables, images, and nested fields.

Reporting: Conditional Formatted Text Blocks

A conditional text block is a range of formatted text that is rendered, if a specified condition is fulfilled. This text block can consist of anything that is supported by TX Text Control: Formatted text, tables, images, other merge fields or sub-templates with IncludeText fields.

The advantage of conditional text blocks is that these blocks are part of the same template and not externalized to sub-templates. Template designers can design all possible content in the same template.

The data source of our sample application has a very simple structure:

Class structure

Based on 2 fields, conditional content should be rendered or not. If the field country equals "United States" in the current data record, a defined table should be rendered. Additionally, a specific sentence should be rendered in case the condition is fulfilled. In case the field vip is "True", a header should be inserted into the final report.

The sample data source has 3 records:

<customer>
    <customerID>1</customerID>
    <lastName>Jackson</lastName>
    <firstName>Paul</firstName>
    <country>United States</country>
    <vip>True</vip>
</customer>

<customer>
    <customerID>2</customerID>
    <lastName>Murphey</lastName>
    <firstName>Peter</firstName>
    <country>Germany</country>
    <vip>True</vip>
</customer>

<customer>
    <customerID>3</customerID>
    <lastName>Hollister</lastName>
    <firstName>Phyllis</firstName>
    <country>Italy</country>
    <vip>False</vip>
</customer>

The following illustration shows 3 different results created based on the same template with conditional text blocks.

In order to insert a conditional block, 2 IF-fields are required with a specific format.

{ IF "country" <> "United States" "%%StartIF%%" ""  }
{ IF "country" <> "United States" "%%EndIF%%" ""  }

The content between these 2 IF-fields is removed, if the condition is true. In our case: The content between these 2 fields is rendered, if country equals "United States".

The following screenshot shows the template that is used in this sample. The green-marked areas define conditional text blocks with various conditions.

Template marked

Technically, the IF-fields render unique placeholders when a specific condition is fulfilled. The text between these markers is removed in the DataRowMerged event:

private void mailMerge1_DataRowMerged(object sender,
                TXTextControl.DocumentServer.MailMerge.DataRowMergedEventArgs e)
{
    byte[] data;
    string sPlaceholderStartIF = "%%StartIF%%";
    string sPlaceholderEndIF = "%%EndIF%%";

    using (TXTextControl.ServerTextControl tx =
        new TXTextControl.ServerTextControl())
    {
        tx.Create();
        tx.Load(e.MergedRow,
            TXTextControl.BinaryStreamType.InternalUnicodeFormat);

        foreach (TXTextControl.IFormattedText part in tx.TextParts)
        {
            do
            {
                int start = part.Find(sPlaceholderStartIF, 0,
                    TXTextControl.FindOptions.NoMessageBox);
                int end = part.Find(sPlaceholderEndIF, start,
                    TXTextControl.FindOptions.NoMessageBox);

                if (start == -1)
                    continue;

                part.Selection.Start = start;
                part.Selection.Length = end - start + sPlaceholderEndIF.Length;
                part.Selection.Text = "";
            } while (part.Find(sPlaceholderStartIF, 0,
                TXTextControl.FindOptions.NoMessageBox) != -1);
        }

        tx.Save(out data,
            TXTextControl.BinaryStreamType.InternalUnicodeFormat);
    }

    e.MergedRow = data;
}

Those IF-blocks can be placed everywhere in the template including main text, headers, footers and text frames. Try this on your own and download the sample Visual Studio 2012 project. At least, a TX Text Control .NET for Windows Forms trial version 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: