SnapDocumentServer.SnapMailMerge(Stream, DocumentFormat) Method
Starts rendering a mail-merge document and saving it to a stream in the specified format.
Namespace: DevExpress.Snap
Assembly: DevExpress.Docs.v21.2.dll
NuGet Package: DevExpress.Document.Processor
Declaration
Parameters
Name | Type | Description |
---|---|---|
stream | Stream | A Stream, containing the document bytes. |
format | DocumentFormat | A DocumentFormat structure, specifying the document format. |
Example
The following code uses the Snap Report API to generate a mail-merge report in the context of the DevExpress Office File API.
It is a console application that creates a document and then exports it to an RTF file.
To use the SnapDocumentServer, the project requires references to the following assemblies:
- DevExpress.Data.v21.2.dll
- DevExpress.Docs.v21.2.dll
- DevExpress.Office.v21.2.Core.dll
- DevExpress.RichEdit.v21.2.Core.dll
- DevExpress.Snap.v21.2.Core.dll
To perform mail merge, the application requires a template - a file in the native .SNX format which contains the document layout, specific fields and data source information. The template file must be created beforehand using the Snap Design Surface. For an example of template creation, review the Lesson 3 - Create a Mail Merge Report.
The code handles the SnapDocumentServer.SnapMailMergeRecordStarted and SnapDocumentServer.SnapMailMergeRecordFinished events to make adjustments to the merged document created for the particular data record.
The server loads a mail merge template using the RichEditDocumentServer.LoadDocument method. Subsequently, the code creates the SnapMailMergeExportOptions object and assigns the data source by specifying the IDataSourceContainerOptions.DataSource property. The mail merge operation is performed by calling the SnapDocumentServer.SnapMailMerge method.
SnapDocumentServer server = new SnapDocumentServer();
server.SnapMailMergeRecordStarted += server_SnapMailMergeRecordStarted;
server.SnapMailMergeRecordFinished += server_SnapMailMergeRecordFinished;
server.LoadDocument(templateFileName);
object dataSource = CreateDataSource();
SnapMailMergeExportOptions options = server.Document.CreateSnapMailMergeExportOptions();
options.DataSource = dataSource;
Console.Write("Performing mail merge... ");
server.SnapMailMerge(options, outputFileName, DocumentFormat.Rtf);