Table Extension: Remove Empty Columns After Mail Merge
When you use mail merge to merge repeating blocks, you may end up with blank columns if no data exists for a particular column. Removing empty columns from a table is demonstrated in this example.

When you use mail merge to merge repeating blocks, you may end up with blank columns if no data exists for a particular column. Consider the following JSON data the is used as the data source for the merge process:
[
{
"articles": [
{
"id": 1,
"name": "Product A",
"description": "Description of product A",
"price": 200,
"qty": 5,
"discount": 0.2
},
{
"id": 2,
"name": "Product B",
"description": "Description of product B",
"price": 244,
"qty": 50,
"discount": 0.0
},
{
"id": 3,
"name": "Product C",
"description": "Description of product C",
"price": 677,
"qty": 2,
"discount": 0.5
}
]
}
]
Table Merge Block
The template consists of a simple merge block in the form of a table that contains all the data columns of the JSON data source.
After merging the data into the template, all rows and columns are rendered accordingly.
Missing Data Columns
But what if an entire column is missing values for all data rows in the JSON data? In the following JSON data, all data points for discount are missing.
[
{
"articles": [
{
"id": 1,
"name": "Product A",
"description": "Description of product A",
"price": 200,
"qty": 5
},
{
"id": 2,
"name": "Product B",
"description": "Description of product B",
"price": 244,
"qty": 50
},
{
"id": 3,
"name": "Product C",
"description": "Description of product C",
"price": 677,
"qty": 2
}
]
}
]
The column in the resulting table will be empty:
Extension Method
The following extension method checks columns for empty cells in order to remove the entire column.
public static class TableExtender {
public static void RemoveEmptyColumns(this Table table) {
List<TableColumn> columns = new List<TableColumn>();
foreach (TableColumn col in table.Columns) {
bool foundText = false;
foreach (TableRow row in table.Rows) {
if (row.IsHeader == true) continue;
if (table.Cells[row.Row, col.Column].Text != "") {
foundText = true; break;
}
}
if (!foundText) { columns.Add(col); }
}
foreach (TableColumn column in columns) {
table.Cells[1, column.Column].Select();
table.Columns.Remove();
}
}
}
The method loops through all columns and table rows, except for the table header, to check if the cells are empty. If they are empty, they are removed from the given table. The method can be called on any given Table in a Text
textControl1.Tables.GetItem().Columns.Remove();
When using the Mail
string jsonData = System.IO.File.ReadAllText("data.json");
textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
using (MailMerge mailMerge = new MailMerge()) {
mailMerge.TextComponent = textControl1;
mailMerge.MergeJsonData(jsonData);
}
foreach(TXTextControl.Table table in textControl1.Tables) {
table.RemoveEmptyColumns();
}
The next screenshot shows the removed last column. This column had all data points missing in the JSON data source.
Also See
This post references the following in the documentation:
TX Text Control .NET Server for ASP.NET
- TXText
Control. Table Class - TXText
Control. Server Text Control Class - TXText
Control. Document Server. Mail Merge Class
TX Text Control .NET for Windows Forms
- TXText
Control. Table Class - TXText
Control. Server Text Control Class - TXText
Control. Document Server. Mail Merge Class
TX Text Control .NET for WPF
- TXText
Control. Table Class - TXText
Control. Server Text Control Class - TXText
Control. Document Server. Mail Merge Class
Related Posts
ASP.NETWindows FormsMail Merge
Merging Merge Block Cells Vertically with Matching Content
A merge block repeats defined content and merge fields based on given data rows of a specific child table or object. This demo shows how to vertically merge table cells in case the content is the…
ASP.NETWindows FormsASP.NET Core
Splitting Tables at Bookmark Positions and Cloning Table Headers
This article shows how to split tables at bookmark positions and how to clone table headers in TX Text Control .NET for Windows Forms and TX Text Control .NET Server.
Loading and Processing Excel XLSX Spreadsheet Tables into TX Text Control…
TX Text Control provides a powerful API to load and process Excel spreadsheet tables in .NET applications. This article shows how to load an Excel file and process the tables using TX Text Control…
Text to Table and Table to Text in TX Text Control and C#
TX Text Control provides powerful table features and also full access to text formatting which can be used to create tables from text and vice versa. This article shows how to convert text to…
Inserting MergeBlocks with the DataSourceManager and Applying Table Styles in C#
This article shows how to insert MergeBlocks with the DataSourceManager and how to apply table styles to those tables. The article uses the DocumentServer class to insert MergeBlocks with the…