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