MergeFieldImageDimension Class
Represents an image dimension (i.e. the width or the height) used across a mail merge process.
Inheritance Hierarchy
SystemObject
  Aspose.Words.FieldsMergeFieldImageDimension

Namespace: Aspose.Words.Fields
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.11
Syntax
public class MergeFieldImageDimension

The MergeFieldImageDimension type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleMergeFieldImageDimension(Double)
Creates an image dimension instance with the given value in points.
Public methodCode exampleMergeFieldImageDimension(Double, MergeFieldImageDimensionUnit)
Creates an image dimension instance with the given value and the given unit.
Properties
  NameDescription
Public propertyCode exampleUnit
The unit.
Public propertyCode exampleValue
The value.
Methods
  NameDescription
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodToString (Inherited from Object.)
Remarks
To indicate that the image should be inserted with its original dimension during a mail merge, you should assign a negative value to the Value property.
Examples
Shows how to set the dimensions of merged images.
public void MergeFieldImageDimension()
{
    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");

    // Create a data table for the mail merge
    // The name of the column that contains our image filenames needs to match the name of our merge field
    System.Data.DataTable dataTable = CreateDataTable("Images", "ImageColumn",
        new string[]
        {
            ImageDir + "Aspose.Words.gif",
            ImageDir + "Watermark.png",
            ImageDir + "dotnet-logo.png"
        });

    doc.MailMerge.FieldMergingCallback = new MergedImageResizer(450, 200, MergeFieldImageDimensionUnit.Point);
    doc.MailMerge.Execute(dataTable);

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

/// <summary>
/// Creates a data table with a single column
/// </summary>
private System.Data.DataTable CreateDataTable(string tableName, string columnName, string[] columnContents)
{
    System.Data.DataTable dataTable = new System.Data.DataTable(tableName);
    dataTable.Columns.Add(new System.Data.DataColumn(columnName));

    foreach (string s in columnContents)
    {
        System.Data.DataRow dataRow = dataTable.NewRow();
        dataRow[0] = s;
        dataTable.Rows.Add(dataRow);
    }

    return dataTable;
}

/// <summary>
/// Sets the size of all mail merged images to one defined width and height 
/// </summary>
private class MergedImageResizer : IFieldMergingCallback
{
    public MergedImageResizer(double imageWidth, double imageHeight, MergeFieldImageDimensionUnit unit)
    {
        mImageWidth = imageWidth;
        mImageHeight = imageHeight;
        mUnit = unit;
    }

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

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        args.ImageFileName = args.FieldValue.ToString();
        args.ImageWidth = new MergeFieldImageDimension(mImageWidth, mUnit);
        args.ImageHeight = new MergeFieldImageDimension(mImageHeight, mUnit);

        Assert.AreEqual(mImageWidth, args.ImageWidth.Value);
        Assert.AreEqual(mUnit, args.ImageWidth.Unit);
        Assert.AreEqual(mImageHeight, args.ImageHeight.Value);
        Assert.AreEqual(mUnit, args.ImageHeight.Unit);
    }

    private readonly double mImageWidth;
    private readonly double mImageHeight;
    private readonly MergeFieldImageDimensionUnit mUnit;
}
See Also