Products Technologies Demo Docs Blog Support Company

Mail Merge: Inserting Merge Blocks using the DataSourceManager in C#

This article shows how to insert merge blocks into a document using the DataSourceManager class in C#. Merge blocks are used to repeat a block of content for each data record in a data source. The DataSourceManager class provides information about the available merge blocks in a data source such as JSON.

Mail Merge: Inserting Merge Blocks using the DataSourceManager in C#

In the context of the TX Text Control MailMerge class, a merge block is a feature that allows repeating data structures in documents to be handled during a mail merge operation.

A merge block represents a section of the document that is repeated for each data entry in a data source, such as a table or list. This is particularly useful when you want to merge multiple records into a single document, such as invoices, order lists or reports with multiple line items per document.

When the MailMerge class processes the document, the merge block is repeated for each row in the associated data source (e.g., a DataTable or JSON array).

DataSourceManager

The out-of-the-box TX Text Control interface can be used to insert merge fields and merge blocks, and available drop-down lists are automatically populated with available block names based on a loaded data source. However, if you want to create your own UI or programmatically add merge blocks to a template, this article shows how to use the DataSourceManager to retrieve available merge blocks and their fields.

Merge Blocks in TX Text Control

Getting Merge Blocks Names

This code demonstrates how to automate the process of inserting merge blocks into a document. It loads data from a JSON file, dynamically inserts merge blocks for tables into the JSON data, and then merges this data into the document. Finally, it saves the document as a PDF.

First, by loading a data source, in this case JSON, we need to create an instance of the DataSourceManager.

// Load JSON data from file
 string jsonData = File.ReadAllText("tabledata.json");

 var dataSourceManager = new DataSourceManager();
 dataSourceManager.LoadJson(jsonData); // Load JSON data

The PossibleMergeBlockTables method returns a list of merge block names in a loaded data source. The following code snippet shows how to loop through the list of merge blocks:

foreach (var table in dataSourceManager.PossibleMergeBlockTables)
{
   // table.TableName
}

All available column names for a specific merge block can be retrieved using the Columns property. The following code snippet shows how to create a new MergeBlockInfo object and how to set the column names:

// Create a new MergeBlockInfo object with the table name
 var mergeBlockInfo = new MergeBlockInfo(table.TableName)
 {
     ColumnNames = table.Columns.Select(c => c.ColumnName).ToList()
 };

Inserting Merge Blocks

Here is the full code that shows how to insert a merge block for each available block in a document and how to finally merge the template to create a PDF document.

using TXTextControl;
using TXTextControl.DocumentServer.DataSources;
using TXTextControl.DocumentServer;

using (var tx = new ServerTextControl())
{
    tx.Create();

    // Load JSON data from file
    string jsonData = File.ReadAllText("tabledata.json");

    var dataSourceManager = new DataSourceManager();
    dataSourceManager.LoadJson(jsonData); // Load JSON data

    // Add merge blocks for all tables
    foreach (var table in dataSourceManager.PossibleMergeBlockTables)
    {
        // Create a new MergeBlockInfo object with the table name
        var mergeBlockInfo = new MergeBlockInfo(table.TableName)
        {
            ColumnNames = table.Columns.Select(c => c.ColumnName).ToList()
        };

        // Create a new MergeBlockSettings object with the block type and header flag
        var mergeBlockSettings = new MergeBlockSettings(BlockTemplateType.TableRow, true);


        // Insert the merge block
        dataSourceManager.InsertMergeBlock(tx, mergeBlockInfo, mergeBlockSettings);
    }

    new MailMerge { TextComponent = tx }.MergeJsonData(jsonData);

    tx.Save("results.pdf", StreamType.AdobePDF);
}

When the document is merged, the merge block is repeated for each row in the data source. The resulting document is a PDF file that contains the merged data.

Merge Blocks in TX Text Control

Conclusion

The DataSourceManager class provides a simple way to retrieve merge block names and their fields from a data source. This allows you to programmatically insert merge blocks into a document and merge data from a data source into the document.

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 CoreDOCX

Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX…

This article explores how to use the TX Text Control MailMerge feature in .NET applications on Linux to generate pixel-perfect PDFs from DOCX templates. This powerful combination enables…


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 CoreBackend

Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C#…

This article shows how to create a PDF generation Web API in ASP.NET Core on Linux using TX Text Control .NET Server. The clean architecture is used to create a maintainable and testable solution.


ASP.NETASP.NET CoreMailMerge

Manipulating Table Cells During the MailMerge Process in .NET C#

This article shows how to manipulate table cells during the mail merge process in .NET C#. The FieldMerged event can be used to manipulate the table cells after they are merged.


ASP.NETASP.NET CoreMailMerge

When to Generate Documents Server-Side Instead of Client-Side: A Focus on…

When it comes to document generation, deciding whether to handle the task server-side or client-side is a key architectural decision for any organization. This article discusses the benefits of…