Mail merge is the process of merging data from a data source to a Word template document. The Syncfusion .NET Word Library (DocIO) allows you to generate reports like invoices and payroll by performing mail merge faster in a batch process without Microsoft Office or interop dependencies. The generated reports can be saved as Word document, PDF, HTML, and more.
This example code shows how to perform a simple mail merge in a Word document using the Syncfusion .NET Word Library with just a few lines of code in C#.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
// Open the template Word document
using FileStream fileStreamPath = new FileStream("Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx);
// Define the mail merge field names and values
string[] fieldNames = new string[] { "EmployeeId", "Name", "Phone", "City" };
string[] fieldValues = new string[] { "1001", "Peter", "+122-2222222", "London" };
// Execute the mail merge
document.MailMerge.Execute(fieldNames, fieldValues);
// Save the Word document
using FileStream outputStream= new FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
document.Save(outputStream, FormatType.Docx);
Performs mail merge in several ways for a whole document, region of a document, and regions with hierarchical data.
The library performs mail merge by repeating the entire document automatically for every record in the data source. Multiple documents can also be created from a Word template.
The library performs mail merge by repeating a document region for each record in the data source, automatically adding new rows for tables.
The library generates complex reports by repeating nested regions of document with relational (hierarchical) data. Also, it supports custom join statements to relate one table to another.
The library allows users to customize the merged document with several intuitive options during the mail merge process.
Format the merged text, numeric values, and date-times automatically with the help of field switches.
Customize and format merged text, numeric values, and date-times using a text merge event. This allows for precise editing and styling during the mail merge process.
Users can also place images in a merge field by adding the prefix “Image:” to it.
Customize image data and dynamically load images during mail merge through the image merge event, enhancing flexibility in document generation.
Users can aAutomatically maps merge field names with column names in the data source. Users They can also add custom mappings to handle differences between field names in templates and data sources.
Before clearing the unmerged fields from the document, bind the alternate values such as error messages or other data to the respective fields.
When none of the fields in the group contain data to merge, users also have the option to bind alternative data for that unmerged group.
Retrieve the merge field names and group names from a template document.
Removes the empty paragraphs, empty groups, and merge fields that are were not merged during mail merge.
Easily add each record as a new row in tables where start and end merge fields are in the same cell.
Users can customize the mail merge to skip the merging of a particular image.
Users can start a new page for each group of records when performing group mail merge in a Word document.
Discover resources from our blog and knowledge base to enhance your efficiency in mail merging in Word documents:
Knowledge base
Knowledge base
Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.
Product Updates
Technical Support
Yes, you can use IF and formula fields with merge fields to make decisions during mail merge, then update fields using the .NET Word Library.
Yes, the .NET Word Library supports saving mail-merged documents in PDF, image, HTML, Markdown, and Word formats.
Both work but using a Word editor is recommended for easier design and layout.
Yes, you can automate the process and generate multiple documents in bulk using C#.
Yes, it supports both implicit and explicit relational data for handling diverse sources.
Use the Syncfusion.DocIO.Net.Core NuGet package. Code samples in the documentation make integration simple.
Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion® is proud to hold the following industry awards.