Class MailMergeTemplate
Template for mail merge operations enabling dynamic document generation from data sources. Creates personalized PDFs by merging templates with recipient data (names, addresses, values).
Example - Generate personalized documents:
// From DataTable:
var recipients = new DataTable();
recipients.Columns.Add("Name");
recipients.Columns.Add("Email");
recipients.Rows.Add("John Smith", "john@example.com");
recipients.Rows.Add("Jane Doe", "jane@example.com");
var template = new MailMergeTemplate(recipients);
// Use with DocxToPdfRenderer for mail merge
Inheritance
Namespace: IronPdf.DocX
Assembly: IronPdf.dll
Syntax
public class MailMergeTemplate : Object
Remarks
Data Source Options:
Related Resources:
Constructors
MailMergeTemplate(DataSet)
Creates a mail merge template from a DataSet with multiple related tables. Supports complex mail merge scenarios with master-detail relationships.
Example:
var dataSet = new DataSet();
dataSet.Tables.Add(customersTable);
dataSet.Tables.Add(ordersTable);
var template = new MailMergeTemplate(dataSet);
Declaration
public MailMergeTemplate(DataSet recipientsData)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Data.DataSet | recipientsData | DataSet containing related tables for merge. |
MailMergeTemplate(DataTable)
Creates a mail merge template from a DataTable containing recipient data. Each row represents one recipient, columns represent merge fields.
Example:
var data = new DataTable();
data.Columns.Add("FirstName");
data.Columns.Add("LastName");
data.Columns.Add("Amount");
data.Rows.Add("John", "Smith", "$150.00");
var template = new MailMergeTemplate(data);
Declaration
public MailMergeTemplate(DataTable recipientsData)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Data.DataTable | recipientsData | DataTable with columns matching template merge fields. |
MailMergeTemplate(String)
Creates a mail merge template from JSON-formatted recipient data. Ideal for API integrations and web service data sources.
Example:
string json = @"[
{""Name"": ""John Smith"", ""Email"": ""john@example.com""},
{""Name"": ""Jane Doe"", ""Email"": ""jane@example.com""}
]";
var template = new MailMergeTemplate(json);
Declaration
public MailMergeTemplate(string JsonRecipientsData)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | JsonRecipientsData | JSON array of recipient objects. |
Methods
create<TRecipientsDataModel>(List<TRecipientsDataModel>)
Creates mail merge output from a strongly-typed list of data models. Generic method for type-safe mail merge with custom objects.
Example:
public class Invoice {
public string CustomerName { get; set; }
public decimal Amount { get; set; }
}
var invoices = new List<Invoice> {
new Invoice { CustomerName = "Acme Corp", Amount = 1500.00m },
new Invoice { CustomerName = "Tech Ltd", Amount = 2250.00m }
};
template.create(invoices);
Declaration
public void create<TRecipientsDataModel>(List<TRecipientsDataModel> recipientsDataList)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.List<TRecipientsDataModel> | recipientsDataList | List of data objects for mail merge. |
Type Parameters
| Name | Description |
|---|---|
| TRecipientsDataModel | Type with properties matching merge fields. |