Mail Merge Examples
MailMerge creates documents such as invoices, letters and reports from a template and data. In a WPF application, the template can be the document of the TextControl in your window: the WPF samples assign the control to TextComponent. The official samples show the basics, and further samples written for Windows Forms or TX Text Control .NET Server show techniques with the same MailMerge class.
WPF Samples
The setup installs these samples, and TXSamples_WPF_CSharp and TXSamples_WPF_VB.NET contain them for version 32.0, for the .NET Framework 4.8 and .NET 6:
| Example | What it shows | Where |
|---|---|---|
| Simple merge | Merge fields inserted from menu items built from the columns of an XML data set, then filled with Merge | TXSamples_WPF_CSharp, folder Howto\MailMerge\MailMerge_Simple |
| Merge blocks | A DOCX template with a merge block, a section repeated for each record, filled with the orders of a data set | TXSamples_WPF_CSharp, folder Howto\MailMerge\MailMerge_Blocks |
| Nested merge blocks | A report with blocks inside blocks, filled from related tables, and a progress bar updated in BlockRowMerged | TXSamples_WPF_CSharp, folder Howto\MailMerge\MailMerge_Nested_Blocks |
| TX Words Reporting | Templates for an invoice, a packing list and a shipping label, designed with a sample database in the reporting tab of the ribbon and merged with the DataSourceManager that RibbonReportingTab.DataSourceManager returns | TXSamples_WPF_CSharp, folder TX Words |
The export dialog of the Reporting sample on GitHub saves DOCX files with StreamType.SpreadsheetML, the Excel format; for DOCX, use StreamType.WordprocessingML.
Other Samples
These samples were written for other products. Their MailMerge code is the same in WPF; replace a Windows Forms TextControl with the WPF TextControl or a ServerTextControl:
| Example | What it shows | Where |
|---|---|---|
| Dynamic contracts | Sections of a contract template included or left out according to JSON data, as described in Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET. A console application for TX Text Control .NET Server, .NET 8, version 33.0 | TXTextControl.Server.NDA.Dynamic |
| Mustache placeholders | A class that turns {{placeholders}} into merge fields and merge blocks, as described in Mail Merge: Merging Templates with Mustache Syntax in C#. A class without a project | MustacheMatcher |
| Parallel merging | Documents merged in separate processes at the same time, as described in True Parallel Processing of Documents using MailMerge, which says the concept applies to WPF as well. Windows Forms, .NET Framework 4.8, version 30.0 | TextControl.WindowsForms.Parallel |
| DOCVARIABLE fields | DOCVARIABLE fields of MS Word documents converted into merge fields, as described in Reporting: Merging MS Word Documents with DocVariables. Windows Forms, .NET Framework 4.8, version 34.0 | TextControl.MailMerge.DocVariables |
| Formatted HTML | HTML from the data merged into merge fields with its formatting, as described in Reporting: Merge Formatted HTML into Merge Fields. Windows Forms, .NET Framework 4.0, version 34.0 | TextControl.MailMerge.MergeFormattedHTML |
| Events for custom processing | Fields, rows and block rows processed in the merge events, as described in Reporting: Custom Processing Using MailMerge Events. Windows Forms, .NET Framework 4.0, version 33.0 | TextControl.MailMerge.CustomProcessingWithEvents |
| Business objects | Collections of business objects merged into merge blocks and nested merge blocks, with totals calculated in the objects instead of the template, as described in Advantages of Using Business Objects with Text Control Reporting. Windows Forms, .NET Framework 4.8, version 33.0 | TextControl.MailMerge.BusinessObjectsUsage |
| Images from files | Image placeholders merged from file names in the data, with the folder of the images set in SearchPath, as described in Reporting: Merging Images from Files Using SearchPath. Windows Forms, .NET Framework 4.0, version 33.0 | TextControl.MailMerge.MergeImagesFromFiles |
| Empty table rows | Alternating row colors from NEXT fields, and empty rows at the end of a merge block removed in BlockRowMerged, as described in Reporting: Removing Empty Table Rows. Windows Forms, .NET Framework 4.5, version 33.0 | TextControl.MailMerge.RemoveEmptyTableRows |
Blog Articles With Code
| Example | What it shows | Where |
|---|---|---|
| Sorting and filtering | The rows of merge blocks sorted and filtered | Filtering and Sorting Repeating Blocks in MailMerge using C# |
| Calculating business objects | Business objects that calculate their own totals, merged into a template | Merging Self-Calculating Business Objects with TX Text Control MailMerge in C# |
| Merge blocks in code | Merge blocks inserted with the DataSourceManager | Mail Merge: Inserting Merge Blocks using the DataSourceManager in C# |
Next Steps
- Mail Merge and Reporting: templates, data sources and merge blocks, explained.
- TX Words WPF Sample Application: the Reporting sample and the other TX Words samples.
- PDF Examples: merged documents saved as PDF and electronic invoices.