ImageFieldMergingArgsImage Property
Specifies the image that the mail merge engine must insert into the document.

Namespace: Aspose.Words.MailMerging
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 20.1
Syntax
public Image Image { get; set; }

Property Value

Type: Image
Examples
Shows how to set which images to merge during the mail merge.
public void MergeFieldImages()
{
    Document doc = new Document();

    // Insert a merge field where images will be placed during the mail merge
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.InsertField("MERGEFIELD Image:ImageColumn");

    // When we merge images, our data table will normally have the full e. of the images we wish to merge
    // If this is cumbersome, we can move image filename logic to another place and populate the data table with just shorthands for images
    DataTable dataTable = CreateDataTable("Images", "ImageColumn",
        new string[]
        {
            "Aspose logo",
            ".Net logo",
            "Watermark"
        });

    // A custom merging callback will contain filenames that our shorthands will refer to
    doc.MailMerge.FieldMergingCallback = new ImageFilenameCallback();
    doc.MailMerge.Execute(dataTable);

    doc.Save(ArtifactsDir + "Field.MergeFieldImages.docx");
}

/// <summary>
/// Image merging callback that pairs image shorthand names with filenames.
/// </summary>
private class ImageFilenameCallback : IFieldMergingCallback
{
    public ImageFilenameCallback()
    {
        mImageFilenames = new Dictionary<string, string>();
        mImageFilenames.Add("Aspose logo", ImageDir + "Aspose.Words.gif");
        mImageFilenames.Add(".Net logo", ImageDir + "dotnet-logo.png");
        mImageFilenames.Add("Watermark", ImageDir + "Watermark.png");
    }

    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        throw new NotImplementedException();
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        if (mImageFilenames.ContainsKey(e.FieldValue.ToString()))
        {
            #if NETFRAMEWORK
            e.Image = Image.FromFile(mImageFilenames[e.FieldValue.ToString()]);
            #else
            e.Image = SKBitmap.Decode(mImageFilenames[e.FieldValue.ToString()]);
            e.ImageFileName = mImageFilenames[e.FieldValue.ToString()];
            #endif
        }

        Assert.NotNull(e.Image);
    }

    private readonly Dictionary<string, string> mImageFilenames;
}
See Also