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.

In the context of the TX Text Control Mail
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 Mail
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 Data
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 Possible
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 Merge
// 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.
Conclusion
The Data
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.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
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…
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.
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.
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.
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…