How to mail merge an image

Hello.
I didnt see such example in the documentation section.
Please describe what i need to do.
How i need to add merge field for an image in docx file.
What code to write to put and image from byte[].
As for example i have such class:

class Vehicle
{
    public byte[] Photo {get; set;}
}

Thank you

Hi Alexander,

Thanks
for your inquiry. You can use the MailMerge.FieldMergingCallback
event that occurs during mail merge when an image mail merge field is
encountered in the document. An image mail merge field is a merge field
named like Image:MyFieldName. You can respond to this event to return a
file name, stream, or an Scaled Image object to the mail merge engine so
that it is inserted into the document. For more information, please
read the following documentation link.
https://docs.aspose.com/words/net/types-of-mail-merge-operations/

To insert an image mail merge field into a document in Word, select Insert/Field command, then select MergeField and type Image:MyFieldName. Please check following code example for your kind reference.

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

If you want to pass image file name(path of image), please use the ImageFieldMergingArgs.ImageFileName property. This property sets the file name of the image that the mail merge engine must insert into the document.Please check the following code example.

Document doc = new Document(MyDir + "in.docx");
doc.MailMerge.FieldMergingCallback = new MailMergeInsertImage();
doc.MailMerge.Execute(
new string[] { "MyFieldName" },
new object[] { MyDir + "image.jpg" });
doc.Save(MyDir + "Out.docx");
public class MailMergeInsertImage : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        e.ImageFileName = e.FieldValue.ToString();
    }
}

Hope this helps you. Please let us know if you have any more queries.

Thank you Tahir.
You pointed me to the correct direction.
I dont have a datatable to auto-take the image from the source, so i wrote such peace of code:

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
    if (args.FieldName.Equals("VehicleImage"))
    {
        if (Vehicle.Photo != null)
        {
            var imageStream = new MemoryStream(Vehicle.Photo.GetImageAsByteArray());
            imageStream.Position = 0;
            // Now the mail merge engine will retrieve the image from the stream.
            args.ImageStream = imageStream;
        }
    }
}

BTW, dont know how to style my comment.
But i have one more question. Is there are any way to limit the size of the image merged ? To set it position (align) ?
I ask this because the image i want to put is pretty big, but i want something like thumbnail.

In meanwhile, i resize the image before mail merge

Hi Alexander,

Thanks
for your inquiry. You can resize the image in ImageFieldMerging method using following code snippet. I suggest you please read the members of ImageFieldMergingArgs class from here.

public class MailMergeInsertImage : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        e.ImageFileName = e.FieldValue.ToString();
        MergeFieldImageDimension imagesize = new MergeFieldImageDimension(50.0);
        e.ImageWidth = imagesize;
        e.ImageHeight = imagesize;
    }
}

*alt7alt7:

BTW, dont know how to style my comment.*

It would be great if you please share some detail about this query along with expected output document. We will then provide you more information about your query.

Thank you Tahir… i did everything i need with your help.

I meant to style a comment here, at the forum. Like you have C# code stylized similar to VS style

Hi Alexander,

Thanks
for your inquiry. You can achieve this by following simple two steps.

  1. Copy the C# code from Visual Studio and paste it in MS Word
  2. Copy code from MS Word and paste it in forum reply/edit window.

Please let us know if you have any more queries.