IFieldMergingCallbackImageFieldMerging Method
Called when the Aspose.Words mail merge engine is about to insert an image into a merge field.

Namespace: Aspose.Words.MailMerging
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.12
Syntax
void ImageFieldMerging(
	ImageFieldMergingArgs args
)

Parameters

args
Type: Aspose.Words.MailMergingImageFieldMergingArgs
Examples
Shows how to insert images stored in a database BLOB field into a report.
Document doc = new Document(MyDir + "MailMerge.MergeImage.doc");

    // Set up the event handler for image fields.
    doc.MailMerge.FieldMergingCallback = new HandleMergeImageFieldFromBlob();

    // Open a database connection.
    string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DatabaseDir + "Northwind.mdb";
    OleDbConnection conn = new OleDbConnection(connString);
    conn.Open();

    // Open the data reader. It needs to be in the normal mode that reads all record at once.
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM Employees", conn);
    IDataReader dataReader = cmd.ExecuteReader();

    // Perform mail merge.
    doc.MailMerge.ExecuteWithRegions(dataReader, "Employees");

    // Close the database.
    conn.Close();

    doc.Save(ArtifactsDir + "MailMerge.MergeImage.doc");
}

private class HandleMergeImageFieldFromBlob : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        // Do nothing.
    }

    /// <summary>
    /// 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.
    /// </summary>
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // The field value is a byte array, just cast it and create a stream on it.
        MemoryStream imageStream = new MemoryStream((byte[])e.FieldValue);
        // Now the mail merge engine will retrieve the image from the stream.
        e.ImageStream = imageStream;
    }
}
See Also