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
- Program.cs (VB: Program.vb)
- ImageStreamProvider.cs (VB: ImageStreamProvider.vb)
- NorthwindDataProvider.cs (VB: NorthwindDataProvider.vb)
More Examples
- How to: Embed Images into a Mail Merge Template
- How to: Implement Mail Merge in a RichEditControl
- How to: Use Multiple Data Sources for a Mail Merge
- How to: Send a Mail-Merge Document as an E-mail
- How to: Import HTML Files that Contain Images Referenced with a Custom Prefix
Documentation
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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
}
}
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
}
}
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;
}
}
}
}