Filtering and Sorting Repeating Blocks in MailMerge using C#
TX Text Control MailMerge's ability to filter and sort repeating merge blocks is a key strength, making it ideal for creating dynamic reports, lists, and catalogs.

One of the most powerful features of TX Text Control Mail
Sample Inventory Report
The result of the following sample is an inventory report that can be filtered by different columns and values.
In this post, we will explore this concept using a practical example: An inventory report based on the following JSON data:
{
"warehouse_id": 1,
"location": "Main Warehouse",
"prepared_by": "John Doe",
"date_prepared": "2023-10-01",
"inventory": [
{
"id": 1,
"product_name": "WD Black 4TB HDD",
"inventory_count": 126,
"projected_sales_per_week": 44,
"unit_price": 364.10
},
{
"id": 2,
"product_name": "Seasonic 850W PSU",
"inventory_count": 363,
"projected_sales_per_week": 28,
"unit_price": 354.86
},
{
"id": 3,
"product_name": "AMD Radeon RX 7900 GPU",
"inventory_count": 476,
"projected_sales_per_week": 7,
"unit_price": 117.48
}
...
]
}
The inventory array contains a list of all items in the warehouse. In many reporting scenarios, it is not necessary to show the entire list; only the relevant items should be highlighted. That's where filtering and sorting come in.
Merging Code Example
The following simple code is used to load the template, merge the JSON data, and export the PDF. Since filtering is done at the template level, nothing special needs to be done in the code.
using TXTextControl;
using TXTextControl.DocumentServer;
using var tx = new ServerTextControl();
tx.Create();
tx.Load("inventory_report.tx", StreamType.InternalUnicodeFormat);
var jsonData = await File.ReadAllTextAsync("pc_hardware_inventory.json");
var mailMerge = new MailMerge { TextComponent = tx };
mailMerge.MergeJsonData(jsonData);
tx.Save("results.pdf", StreamType.AdobePDF);
Learn More
Merge blocks can be filtered and sorted using linked instructions that shape the data used in each block. This article demonstrates how to add sorting and filtering instructions to existing merge blocks.
Adding Sorting and Filter Instructions to Existing MergeBlocks
Filtering Repeating Merge Blocks
With TX Text Control, you can apply conditions to repeating blocks, ensuring that only items that match a rule are rendered in the document. For example:
- Low stock report: Show only items where inventory_count < 50
- High value items: Show only items where price > 1000.
- Fast movers: Show only items where projected_sales_per_week > 40.
This is accomplished in the MailMerge template by applying filter conditions to the merge block. Only matching records are included when the JSON data is merged.
You can design a template table for inventory items and configure it to display products with an inventory count of less than 100. The result is an automatically generated "Reorder Report" that doesn't require any post-processing code.
Our sample data set contains 100 entries, and we will now filter out all items with an inventory count of less than 100. A summary section using formulas to count the results and other calculated data has been added to the end of the report.
According to our sample data, there are 22 records that have low inventory. To improve the visual appeal for readers, we added an IF condition to the cell that displays "LOW STOCK" in red when the inventory count is less than 50.
Sorting Repeating Merge Blocks
In addition to filtering, you can sort the results dynamically. Some examples:
- Sort by unit_price (descending): Highlight the most expensive products at the top.
- Sort by inventory_count (ascending): Quickly see which items are running out.
- Sort by projected_sales_per_week (descending): Focus on the products with the highest demand.
Sorting can be combined with filtering. For instance, you might filter for inventory_count < 100 and then sort by projected_sales_per_week DESC to see which low-stock items are selling fastest.
Why This Matters
The concept of filtering and sorting inside MailMerge has several advantages:
- No post-processing required
The document engine automatically removes or reorders records, so developers don't need to write custom loops in code. - Dynamic and reusable templates
A single template can serve different purposes simply by changing the filter and sort rules. For example, the same inventory table could generate a low-stock report, a high-value catalog, or a list of fast-moving sales. - End-user empowerment
Business users, not developers, can control how data is presented in documents by designing templates with filters and sorting rules. - Performance and consistency
The highly optimized MailMerge engine ensures consistent and predictable data manipulation results across reports.
Conclusion
TX Text Control MailMerge's ability to filter and sort repeating merge blocks makes it an incredibly flexible tool for real-world reporting scenarios. Whether you need a low-stock list, a high-value catalog, or a sales overview of your fastest-moving products, you can create it with a single template and your existing data.
This feature enables developers and business users to create smart, data-driven documents without the need for complex logic in the application layer, resulting in cleaner code and faster workflows.
Related Posts
Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET
This article demonstrates how to generate dynamic NDAs using TX Text Control MailMerge in C# .NET. It covers the process of creating a template, binding data, and generating the final document.
Text Control at NDC Copenhagen Developers Festival 2025
Join Text Control at the 2025 NDC Copenhagen Developers Festival, where we will present our newest innovations and solutions for document processing, reporting, and PDF generation. This unique…
Why HTML is not a Substitute for Page-Oriented Formats like DOCX
In this blog post, we will discuss the limitations of HTML as a document format and explain why page-oriented formats, such as DOCX, remain essential for certain use cases. We will explore the…
Back from KCDC 2025 - Some Impressions
In this blog post, we will share our impressions of the KCDC 2025 conference and our key takeaways from it. We showcased our latest features and collected valuable feedback from attendees.
TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…