Mail merge image getting null value

why am i getting e.FieldValue == null in ImageFieldMerging Method?
if I do it in FieldMerging i’m getting a correct value

source is DataTable, doc.MailMerge.Execute(table);

class HandleMergeImageField : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        // Do nothing.
        if (args.FieldName.EndsWith("_img"))
        {
            DocumentBuilder _b = new(args.Document);
            _b.MoveToMergeField(args.FieldName);
            Shape _shape = _b.InsertImage((byte[])args.FieldValue);
            _shape.Height = 100;
            _shape.Width = 250;

        }
    }
    // / 
    // / 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;
        if (stream!=null)
        {
            stream.Position = 0;
            // Now the mail merge engine will retrieve the image from the stream.
            e.ImageStream = stream;
        }
    }
}

@erikg You should use merge field with a special name Image:FieldName and used image path, image bytes or image url as a value of FieldName. For example see the following code and template: in.docx (12.2 KB)

string[] fieldNames = new string[] { "FromUrl", "FromPath", "FromBytes" };
object[] fieldValues = new object[] {
    @"https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png",
    @"C:\Temp\aspose-logo.png",
    File.ReadAllBytes(@"C:\Temp\aspose-logo.png")
};

Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.Execute(fieldNames, fieldValues);
doc.Save(@"C:\Temp\out.docx");

i think my field names are correct. i’m using datatable as a datasource and in the “cell” value tried to put local path and bytes, but i always get null value

@erikg Unfortunately, I cannot reproduce the problem on my side. I Have used the following simple code for testing (Template is the same as I have attached in my previous answer).

string[] fieldNames = new string[] { "FromUrl", "FromPath", "FromBytes" };
object[] fieldValues = new object[] {
    @"https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png",
    @"C:\Temp\aspose-logo.png",
    File.ReadAllBytes(@"C:\Temp\aspose-logo.png")
};

Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.FieldMergingCallback = new TestFieldMergingCallback();
doc.MailMerge.Execute(fieldNames, fieldValues);
doc.Save(@"C:\Temp\out.docx");
private class TestFieldMergingCallback : IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        Console.WriteLine("FieldMerging");
        Console.WriteLine(args.FieldName);
        Console.WriteLine(args.FieldValue == null);
        Console.WriteLine("==================================");
    }

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        Console.WriteLine("ImageFieldMerging");
        Console.WriteLine(args.FieldName);
        Console.WriteLine(args.FieldValue == null);
        Console.WriteLine("==================================");
    }
}

The output of the code is the following:

ImageFieldMerging
FromUrl
False
==================================
ImageFieldMerging
FromPath
False
==================================
ImageFieldMerging
FromBytes
False
==================================

Could you please create a simple console application that will allow us to reproduce the problem? We will check it and provide you more information.

it’s working now. i had also named fields in datatable “Image:…”
another Q: is it possible to "declare method public void ImageFieldMerging as async Task

@erikg

No, unfortunately, there is no no way to declare ImageFieldMerging method as async Task.

1 Like