In the context of the TX Text Control Mail
╰ DocumentServer Namespace
╰ MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services. 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 Mail
╰ DocumentServer Namespace
╰ MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services. 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 Data
╰ DocumentServer.DataSources Namespace
╰ DataSourceManager Class
The DataSourceManager class is designed for handling all existing kinds of data sources which can be used together with the MailMerge class. to retrieve available merge blocks and their fields.
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
╰ DocumentServer.DataSources Namespace
╰ DataSourceManager Class
╰ PossibleMergeBlockTables Property
Gets a DataTableInfoCollection of DataTableInfo objects representing the tables which can be used as merge blocks using the currently selected master table. 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 ╰ TX Text Control .NET Server for ASP.NET
╰ DocumentServer.DataSources Namespace
╰ DataTableInfo Class
╰ Columns Property
Gets a DataColumnInfoCollection representing the columns of this data table. property. The following code snippet shows how to create a new Merge
╰ DocumentServer.DataSources Namespace
╰ MergeBlockInfo Class
The MergeBlockInfo class is used to insert a table or paragraph based repeating merge block into a TextControl instance using the DataSourceManager.InsertMergeBlock method. 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.
Conclusion
The Data
╰ DocumentServer.DataSources Namespace
╰ DataSourceManager Class
The DataSourceManager class is designed for handling all existing kinds of data sources which can be used together with the MailMerge class. 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.