Merge image: returning System.Byte[] rather than image

Hello
We are attempting to use words mailmerge to insert text and an image into word document. The text is working properly, however trying to insert to a byte array into the field and by using the <> merge field tag is not. Our issue is that it gets replaced by what looks like the string representation of the byte array rather than the text (system.Byte[]).
There are several items that leads me to believe that the issue is that words is not recognizing the Image: prefix.

  1. The event handler is not being fired during the mail merge
  2. When I change the tag from <> to <>, I receive the same system.Byte[] string in place of an image.
    The code that is producing the issue can be found below, however is just a small piece of the total code.
Private Function createSubmissionDocument(ByVal templateFields As Entities.templateItems, ByVal templateFile As String) As Document
Dim doc As Document
Dim dalDocProcessor As DAL.DocProcessor
Dim docStream As FileStream
Try
'load template file
dalDocProcessor = New DAL.DocProcessor
docStream = dalDocProcessor.GetDocumentStream(templateFile)
'load the stream into a aspose words class
doc = New Document(docStream)
If templateFields.Count > 0 Then
AddHandler doc.MailMerge.MergeImageField, AddressOf HandleMergeImage
'Fill the fields in the document with user data.
doc.MailMerge.Execute(templateFields.getTemplateKeys, templateFields.getTemplateValues)
End If
Catch ex As Exception
Throw ex
End Try
Return doc
End Function
' This is called when mail merge engine encounters Image:XXX merge field in the document.
' You have a chance to return an Image object, file name or a stream that contains the image.
Private Sub HandleMergeImage(ByVal sender As Object, ByVal e As MergeImageFieldEventArgs)
Dim imageStream As MemoryStream = New MemoryStream(CType(e.FieldValue, Byte()))
' Now the mail merge engine will retrieve the image from the stream.
e.ImageStream = imageStream
End Sub

Thanks
Shawn

Hi
Thanks for your inquiry. Unfortunately I can’t reproduce your problem. I used the following code for testing. The template is attached.

Sub Main()
'create names array
Dim names As String() = {"Name", "myImage"}
'create values array
Dim values As Object() = {"Ben", File.ReadAllBytes("myImage.jpg")}
Dim doc As Document = createSubmissionDocument(names, values, "in.doc")
doc.Save("out.doc")
End Sub
Private Function createSubmissionDocument(ByVal fieldNames As String(), ByVal fieldValues As Object(), ByVal templateFile As String) As Document
'load template file
Dim doc As Document = New Document(templateFile)
Try
'add MergeImageField event handler
AddHandler doc.MailMerge.MergeImageField, AddressOf HandleMergeImage
'Fill the fields in the document with user data.
doc.MailMerge.Execute(fieldNames, fieldValues)
Catch ex As Exception
Throw ex
End Try
Return doc
End Function
' This is called when mail merge engine encounters Image:XXX merge field in the document.
' You have a chance to return an Image object, file name or a stream that contains the image.
Private Sub HandleMergeImage(ByVal sender As Object, ByVal e As MergeImageFieldEventArgs)
Dim imageStream As MemoryStream = New MemoryStream(CType(e.FieldValue, Byte()))
' Now the mail merge engine will retrieve the image from the stream.
e.ImageStream = imageStream
End Sub

Best regards.

this sounds to me like the merge field on the template is not set up correctly. For the merge field try this {MERGEFIELD Image:BenImage \* MERGEFORMAT}
if you leave off the “Image:” you will get the text “System.Byte[]” showing on your document

Hi
Could you please attach your template here for testing?
Best regards.

Hello
I got it to work by copying your mergefield. Mine looked correct, however there must have been some error with it.
Thanks

One thing thats strange about the mergefields is that if you don’t toggle the mergefields before editing then the change you make is visual only.
As a solution, I always right click the merge field, then click toggle field codes, edit the field then right click again on the merge field and click update field.

Hi
Thanks for your remark. I do this in the same way.
Best regards.

Hi I get a “Parameter is not valid” error when I use the above code but using “doc.MailMerge.ExecuteWithRegions” method.

this is when I use the Image:myImage merge field. It returns “System.Byte[]” if I use <>

can you help please?

Hi
Thanks for your inquiry. I tried to reproduce your issue but all works fine on my side. Here is code I used for testing.

// Create dummy data source
DataTable myTable = new DataTable("myTable");
// Add columns
myTable.Columns.Add("Name", typeof(string));
myTable.Columns.Add("myImg", typeof(byte[]));
// Add few rows
myTable.Rows.Add(new object[] { "First Image", File.ReadAllBytes(@"Test110\test1.jpg") });
myTable.Rows.Add(new object[] { "Second Image", File.ReadAllBytes(@"Test110\test2.jpg") });
myTable.Rows.Add(new object[] { "Third Image", File.ReadAllBytes(@"Test110\test3.jpg") });
// Open template document
Document doc = new Document(@"Test110\in.doc");
// Execute mail merge with regions
doc.MailMerge.ExecuteWithRegions(myTable);
// Save document
doc.Save(@"Test110\out.doc");

Could you please provide me your code and document for testing?
Best regards.

I had the same problem, but I’ve found the solution: if your image field is myImage, then insert into the doc a merge field named myImage, after that right click the merge field -> Toggle Field Codes and here insert Image: before your field name; the text should look like {MERGEFIELD Image:myImage * MERGEFORMAT}. After that toggle back the field and save. It works!