Products Technologies Demo Docs Blog Support Company

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.

MailMerge: Conditional Table Cell Colors using Filter Instructions

The TXTextControl.DocumentServer.MailMerge class provides an extensible framework to inject custom logic to the merge process. The TXTextControl.DocumentServer.MailMerge.FieldMerged event can be used to manipulate the results, but also gives access to the surrounding instance of the TXTextControl.TableCell class.

This combination allows creative implementations such as conditional table cell colors based on specific filter instructions.

In the following sample, the table cell Qty should be highlighted red, if quantity is higher than 10, otherwise it should be green.

Template before the merge

These conditions can be adjusted in the following dialog:

Custom dialog

When merging the document, the conditions are evaluated in the custom events:

Merge results

The class CellFilterInstructions contains the instructions for the custom filter:

public class CellFilterInstructions
{
  public double? CompareValue { get; set; } = null;
  public RelationalOperator? Operator { get; set; } = null;
  public Color TrueColor { get; set; } = Color.White;
  public Color FalseColor { get; set; } = Color.White;

  public enum RelationalOperator
  {
      Equals = 0,
      NotEqual,
      LessThan,
      GreaterThan,
  }
}

This class is serialized as JSON and stored in the TXTextControl.TableCell.Name property. When a new dialog is opened, the JSON is deserialized and passed to the dialog. After changes are made, the object is stored as a serialized JSON string back in the TableCell.Name property.

// create a new dialog form
frmCellFormat frmCellFormat = new frmCellFormat();

// check, if there are existing filter instructions
CellFilterInstructions cellFilterInstructions = 
    (CellFilterInstructions)JsonConvert.DeserializeObject(
        textControl1.Tables.GetItem().Cells.GetItem().Name, 
        typeof(CellFilterInstructions));

if (cellFilterInstructions == null)
{
    // create new instructions
    Color cCellBackColor = textControl1.Tables.GetItem().Cells.GetItem().CellFormat.BackColor;

    cellFilterInstructions = new CellFilterInstructions()
    {
        TrueColor = cCellBackColor,
        FalseColor = cCellBackColor
    };
}

// show the form
frmCellFormat.CellFilterInstructions = cellFilterInstructions;

if (frmCellFormat.ShowDialog() == DialogResult.OK)
{
    // save the instructions in the Name property
    textControl1.Tables.GetItem().Cells.GetItem().Name =
        JsonConvert.SerializeObject(frmCellFormat.CellFilterInstructions); 
}

In the FieldMerging event, the instructions are evaluated and the custom background color is applied to the cell that is returned in the event arguments:

private void MailMerge_FieldMerged(object sender, 
                                   TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs e)
{
    // custom field handling
    if (e.TableCell == null)
        return;

    // if TableCell.Name has instructions, create a CellFilterInstructions object
    // and evaluate the instructions and set the table cell color
    if (e.TableCell.Name != "")
    {
        CellFilterInstructions instructions = 
            (CellFilterInstructions)JsonConvert.DeserializeObject(
                e.TableCell.Name,
                typeof(CellFilterInstructions));

        // retrieve the color
        Color? color = instructions.GetColor(e.MailMergeFieldAdapter.ApplicationField.Text);

        // apply the color
        if(color != null)
            e.TableCell.CellFormat.BackColor = (Color)color;
    }
}

Test this sample on your own and download the GitHub repository.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.DocumentServer.MailMerge Class
  • TXTextControl.DocumentServer.MailMerge.FieldMerged Event
  • TXTextControl.TableCell Class
  • TXTextControl.TableCell.Name Property

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 2017 or better
  • TX Text Control .NET for Windows Forms (trial sufficient)

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

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.


ASP.NETReportingWindows Forms

Different Ways to Create Documents using Text Control Products

There are many ways to create documents using Text Control products. This article gives an overview of different approaches using our products.


ActiveXASP.NETReporting

TX Text Control 32.0 Has Been Released

We are pleased to announce the immediate availability of TX Text Control 32.0 for all platforms including ASP.NET, Windows Forms, WPF and ActiveX.


ASP.NETWindows FormsWPF

An Ultimate Guide to Mail Merge with MS Word Documents in C#

The MailMerge class provides very effective ways to merge data into MS Word compatible templates. This updated ultimate guide provides an overview of all the important features and functionalities…


ActiveXASP.NETReporting

TX Text Control 31.0 and TX Spell .NET 10.0 Have Been Released

We are happy to announce the immediate availability of TX Text Control 31.0 for all platforms including ASP.NET, Windows Forms, WPF and ActiveX and TX Spell .NET 10.0 for all .NET based platforms.