Example T1280094
Visible to All Users

Word Processing Document API - Embed Images into a Mail Merge Template

This example implements the IUriStreamProvider to insert images from a database.

Implementation Details

The INCLUDEPICTURE field in the template has a nested MERGEFIELD that refers to the CategodyID field from the database. The "dbimg://" prefix is a stream generated by the IUriStreamProvider implementation. The GetStream method parses the received URI (the INCLUDEPICTURE field), finds the required data row, and returns the MemoryStream with an image.

Tip:
You can use a DOCVARIABLE field to insert images to a template. Refer to the following topic for more information: Handle the CalculateDocumentVariable Event to Insert a Document Element

Files to Review

More Examples

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Program.cs(vb)
C#
using DevExpress.Office.Services; using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.API.Native; using System; using System.Diagnostics; using System.Windows.Forms; namespace RichEditImageMailMerge { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer()) { RegisterUriStreamService(wordProcessor); wordProcessor.LoadDocument(Application.StartupPath + @"MailMergeTemplate.rtf"); wordProcessor.Options.MailMerge.DataSource = NorthwindDataProvider.Categories; wordProcessor.Options.MailMerge.ViewMergedData = true; MergeToNewDocument(wordProcessor); } } #region #registerprovider private static void RegisterUriStreamService(RichEditDocumentServer richEditDocumentServer) { IUriStreamService uriStreamService = richEditDocumentServer.GetService<IUriStreamService>(); uriStreamService.RegisterProvider(new ImageStreamProvider(NorthwindDataProvider.Categories, "Picture")); } #endregion #registerprovider #region Mail-merge the document private static void MergeToNewDocument(RichEditDocumentServer richEditDocumentServer) { MailMergeOptions options = richEditDocumentServer.Document.CreateMailMergeOptions(); options.MergeMode = MergeMode.NewSection; string fileName = System.IO.Directory.GetCurrentDirectory() + @"MailMergeResult.rtf"; richEditDocumentServer.Document.MailMerge(options, fileName, DocumentFormat.Rtf); var p = new Process(); p.StartInfo = new ProcessStartInfo(fileName) { UseShellExecute = true }; p.Start(); } #endregion Mail-merge the document } }
ImageStreamProvider.cs(vb)
C#
using System.IO; using System.Data; using DevExpress.Office.Services; namespace RichEditImageMailMerge { #region #iuristreamprovider public class ImageStreamProvider : IUriStreamProvider { static readonly string prefix = "dbimg://"; DataTable table; string columnName; public ImageStreamProvider(DataTable sourceTable, string imageColumn) { this.table = sourceTable; this.columnName = imageColumn; } public Stream GetStream(string uri) { uri = uri.Trim(); if (!uri.StartsWith(prefix)) return null; string strId = uri.Substring(prefix.Length).Trim(); int id; if (!int.TryParse(strId, out id)) return null; DataRow row = table.Rows.Find(id); if (row == null) return null; byte[] bytes = row[columnName] as byte[]; if (bytes == null) return null; MemoryStream memoryStream = new MemoryStream(bytes); return memoryStream; } #endregion #iuristreamprovider } }
NorthwindDataProvider.cs(vb)
C#
using System.Data; using System.Data.OleDb; namespace RichEditImageMailMerge { public static class NorthwindDataProvider { private static string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\nwind.mdb;Persist Security Info=False;"; static NorthwindDataProvider() { using (OleDbConnection connection = new OleDbConnection(connectionString)) { OleDbCommand selectCommand = new OleDbCommand("SELECT * FROM Categories", connection); OleDbDataAdapter da = new OleDbDataAdapter(selectCommand); categories = new DataTable("Categories"); da.Fill(categories); categories.Constraints.Add("IDPK", categories.Columns["CategoryID"], true); selectCommand.Dispose(); } } private static DataTable categories; public static DataTable Categories { get { return categories; } } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.