Aspose.Words can generate documents from templates with mail merge fields. The data from an external source like a database or file is placed into these fields and formatted, and the resulting document is saved in the folder you specify.
Mail Merge is a feature of Microsoft Word for creating documents like letters, labels and envelopes quickly and easily. Aspose.Words takes the standard mail merge and advances it many steps ahead, turning it into a full-fledged reporting solution that allows to generate even more complex documents such as reports, catalogs, inventories, invoices.
The advantages of the Aspose.Words reporting solution are:
- Design reports in Microsoft Word, use standard mail merge fields.
- Define regions in the document that grow, such as detail rows of an order.
- Insert images during mail merge.
- Execute any custom logic, control formatting or insert complex content using mail merge event handlers.
- Populate documents with data from any type of data source.
Basic Steps
Steps to perform a mail merge are quite easy. First, you use Microsoft Word to create and design a Word document normally called template. Note that the document does not have to be a Microsoft Word Template (.dot), it can be a normal .doc document. You insert some special fields called merge fields into the template in places where you want data from your data source to be later inserted.
Then you open the document in Aspose.Words and execute a mail merge operation. The mail merge operation will take data from your data source and merge into the document. You can then save the document in Word binary format (.doc) or any other format supported by Aspose.Words. You can save it to a file or stream right to the client browser.
You can also designate repeatable merge regions in the document or insert special merge fields that allow to insert other content such as images.
Depending on how you set up mail merge fields and repeatable regions inside the document, the document will grow to accommodate for multiple records in your data source.
If you do not use mail merge regions, then the mail merge will be similar to Microsoft Word mail merge and whole document content will be repeated for each record in the data source.
Using repeatable mail merge regions, you can designate portions inside a document that will be repeated for each record in the data source. For example, if you mark a table row as a repeatable region then this table row will be repeated causing the table to dynamically grow to accommodate all of you data.
Data Sources
Data can come from a source in a variety of formats supported by ADO.NET. It can be DataTable, DataView, DataSet, IDataReader or an array of values. If you have your data in XML format, then you need to load it into a DataSet and merge with the DataSet.
Aspose.Words also accepts ADO Recordset objects and exposes COM classes and interfaces so you can use it from within COM and Scripting applications such as ASP applications.
Aspose.Words.Reporting Namespace
All the classes and delegates related to mail merge are placed into the separate namespace named Aspose.Words.Reporting.
Example UsingReportingNamespace
Include the following statement in your code if you are using mail merge functionality.
[C#]
using Aspose.Words.Reporting;
[Visual Basic]
Imports Aspose.Words.Reporting