We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert image from xml using mailmerge

How can I insert an image to a word from xml? In the template attached, I want to insert an image in the MyLogo field. How can I achieve this? I have seen examples to achieve this from database but not xml.

My xml would look something like this.
temp.doc attached.

<?xml version="1.0" encoding="utf-8"?>

Hi there,

Thanks for your inquiry. In your case, I suggest you to use IFieldMergingCallback interface to achieve your requirements as shown in following code example.

Please specify a field name prefix like Image:MyFieldName in the document to be able to directly merge images during Mail Merge.

are three properties available ImageFileName, ImageStream and Image to
specify where the image must be taken from
. Set only one of these
properties. Please see the following highlighted code snippet.

Document doc = new
Document(MyDir + “in.docx”);

//Your code...

//Your code...

doc.MailMerge.FieldMergingCallback = new HandleMergeImageField();

doc.MailMerge.Execute(datatable); //Pass the datatable in Execute method

doc.Save(MyDir + "Out.docx");

private class HandleMergeImageField : IFieldMergingCallback


void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)


// Do nothing.



/// 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.


void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)


MemoryStream stream = (MemoryStream)e.FieldValue;

stream.Position = 0;

// Now the mail merge engine will retrieve the image from the stream.

e.ImageStream = stream;