Images and MappedDataFields

I am wondering if the following is possible: Can I display an image using a mapped data field? I know ordinarily you use the format Image:XXX as a merge field name. Here is my situation…

The columns I am attempting to display using the merge fields have guid values. Therefore, if I want to display an image the merge field name must be something like <Image:3BFBAE8A-39CD-4FF5-9220-0D3472E1D760>.

We allow our customers to see the merge fields and assign columns to them. Ideally, I would like prevent cryptic merge field names with guid values. For most other fields, I can simply use a mapped data field to display something more meaningful to the user. For example, they may have a merge field called <>. There are no columns named Location, but in the code I map it to the proper guid.

Is there a way to do this for image fields?

Hi Jacob,

Thanks for your inquiry. Yes sure, in your case, you can display image by using the code like below:

Document doc = new Document(@"c:\temp\In.docx");
doc.MailMerge.Execute(new string[] { "3BFBAE8A-39CD-4FF5-9220-0D3472E1D760"}, new object[] { @"C:\temp\img.jpg" });

Moreover, please read the following article on how to execute simple mail merge:

I hope, this will help.

Best Regards,

Thanks. My images are stored in the database as a byte array. Do you think the same overload will work in my case?

Hi Jacob,

Thanks for your inquiry. Yes sure, you can pass in the image as a byte array, i.e. returned from database, to the same MailMerge.Execute(String[], Object[]) overload.

Best Regards,

I think I am having trouble getting the datasource set using this method. In instances other than images, I have been able to simply pass the datarow to the execute function. I was hoping to be able to assign the mail merge datasource as that datarow, but it appears datasource only accepts a string value.

How can I make the following access the datarow _ValuesRow?

_doc.MailMerge.Execute(New String() {item!DocFldID.ToString}, New Object() {_ValuesRow.Item(item!DocFldID.ToString)})

Hi Jacob,

Thanks for your inquiry.

In case you’re merging data from DataRow, please use MailMerge.Execute(DataRow) overload. Please let me know if I can be of any further assistance.

Best Regards,

Ok, the overload for string array and object array doesn’t seem to work for displaying an image. Am I doing something wrong? It only displays System.Byte[] in my merge field. Is there some type of conversion I need to perform on the byte array?

Hi Jacob,

Thanks for your inquiry. You can pass your DataRow in the MailMerge.Execute method as follows:

Document doc = new Document(@"c:\test\test.docx");

DataRow row = GetDataTable().Rows[0];

private static DataTable GetDataTable()
    DataTable dataTable = new DataTable("tbl");
    dataTable.Columns.Add(new DataColumn("img1", typeof(byte[])));
    dataTable.Columns.Add(new DataColumn("img2", typeof(byte[])));

    MemoryStream stream1 = new MemoryStream();
    MemoryStream stream2 = new MemoryStream();

    using (Image img1 = Image.FromFile(@"c:\test\img1.jpg"))
    using (Image img2 = Image.FromFile(@"c:\test\img2.jpg"))
        img1.Save(stream1, ImageFormat.Jpeg);
        img2.Save(stream2, ImageFormat.Jpeg);

    DataRow dataRow = dataTable.NewRow();
    dataRow[0] = stream1.GetBuffer();
    dataRow[1] = stream2.GetBuffer();


    return dataTable;

Moreover, I have attached test documents here for you to play with.

I hope, this will help.

Best Regards,