Products Technologies Demo Docs Blog Support Company

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.

Filtering and Sorting Repeating Blocks in MailMerge using C#

One of the most powerful features of TX Text Control MailMerge when generating dynamic reports, lists, or catalogs is the ability to filter and sort repeating merge blocks. This feature allows you to do much more than simply insert all items from a data source into a table; it enables you to shape the output exactly the way your users need it.

Sample Inventory Report

The result of the following sample is an inventory report that can be filtered by different columns and values.

Inventory Report with TX Text Control

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.

Filter and Sorting Options

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.

Filter and Sorting Options

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.

Filter and Sorting Options

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:

  1. No post-processing required
    The document engine automatically removes or reorders records, so developers don't need to write custom loops in code.
  2. 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.
  3. End-user empowerment
    Business users, not developers, can control how data is presented in documents by designing templates with filters and sorting rules.
  4. 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.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

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.NETASP.NET CoreContract

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.


ASP.NETASP.NET CorePDF

Validate Digital Signatures and the Integrity of PDF Documents in C# .NET

Learn how to validate digital signatures and the integrity of PDF documents using the PDF Validation component from TX Text Control in C# .NET. Ensure the authenticity and compliance of your…


ASP.NETASP.NET CoreC#

Day-1 Support for .NET 10 in TX Text Control 34.0

Microsoft has officially released .NET 10. TX Text Control 34.0 offers day-one support for .NET 10 and has undergone thorough testing to ensure compatibility with the latest .NET version and…


ASP.NETASP.NET CorePDF

Validate PDF/UA Documents and Verify Electronic Signatures in C# .NET

The new TXTextControl.PDF.Validation NuGet package enables you to validate PDF/UA documents and verify digital signatures directly in your code without relying on third-party tools or external…


ASP.NETASP.NET CoreC#

How To Choose the Right C# PDF Generation Library: Developer Checklist

To make your choice easy, this guide provides a systematic evaluation framework for two library categories: basic and enterprise PDF libraries. It covers matching features to use cases, evaluating…

Summarize this blog post with:

Share on this blog post on: