com.aspose.words

Class MergeFieldImageDimension

  • java.lang.Object
    • com.aspose.words.MergeFieldImageDimension
  • All Implemented Interfaces:
    java.lang.Cloneable
    public class MergeFieldImageDimension 
    extends java.lang.Object

Represents an image dimension (i.e. the width or the height) used across a mail merge process.
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.

Example:

Shows how to set the dimensions of merged images.
@Test
public void mergeFieldImageDimension() throws Exception {
    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
    DataTable dataTable = createDataTable("Images", "ImageColumn",
            new String[]
                    {
                            getImageDir() + "Aspose.Words.gif",
                            getImageDir() + "Watermark.png",
                            getImageDir() + "dotnet-logo.png"
                    });

    doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(450.0, 200.0, MergeFieldImageDimensionUnit.POINT));
    doc.getMailMerge().execute(dataTable);

    doc.updateFields();
    doc.save(getArtifactsDir() + "Field.MergeFieldImageDimension.docx");
}

/// <summary>
/// Creates a data table with a single column
/// </summary>
private DataTable createDataTable(final String tableName, final String columnName, final String[] columnContents) {
    DataTable dataTable = new DataTable(tableName);
    dataTable.getColumns().add(new DataColumn(columnName));

    for (String s : columnContents) {
        DataRow dataRow = dataTable.newRow();
        dataRow.set(0, s);
        dataTable.getRows().add(dataRow);
    }

    return dataTable;
}

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

    public void fieldMerging(final FieldMergingArgs args) {
        throw new UnsupportedOperationException();
    }

    public void imageFieldMerging(final ImageFieldMergingArgs args) {
        args.setImageFileName(args.getFieldValue().toString());
        args.setImageWidth(new MergeFieldImageDimension(mImageWidth, mUnit));
        args.setImageHeight(new MergeFieldImageDimension(mImageHeight, mUnit));

        Assert.assertEquals(mImageWidth, args.getImageWidth().getValue());
        Assert.assertEquals(mUnit, args.getImageWidth().getUnit());
        Assert.assertEquals(mImageHeight, args.getImageHeight().getValue());
        Assert.assertEquals(mUnit, args.getImageHeight().getUnit());
    }

    private double mImageWidth;
    private double mImageHeight;
    private int mUnit;
}
See Also:
MergeFieldImageDimensionUnit

Constructor Summary
MergeFieldImageDimension(doublevalue)
Creates an image dimension instance with the given value in points.
MergeFieldImageDimension(doublevalue, intunit)
Creates an image dimension instance with the given value and the given unit.
 
Property Getters/Setters Summary
intgetUnit()
void
setUnit(intvalue)
           The unit. The value of the property is MergeFieldImageDimensionUnit integer constant.
doublegetValue()
void
setValue(doublevalue)
           The value.
 

    • Constructor Detail

      • MergeFieldImageDimension

        public MergeFieldImageDimension(double value)
        Creates an image dimension instance with the given value in points. You should use a negative value to indicate that the original value of the corresponding image dimension should be applied.
        Parameters:
        value - The value.

        Example:

        Shows how to set the dimensions of merged images.
        @Test
        public void mergeFieldImageDimension() throws Exception {
            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
            DataTable dataTable = createDataTable("Images", "ImageColumn",
                    new String[]
                            {
                                    getImageDir() + "Aspose.Words.gif",
                                    getImageDir() + "Watermark.png",
                                    getImageDir() + "dotnet-logo.png"
                            });
        
            doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(450.0, 200.0, MergeFieldImageDimensionUnit.POINT));
            doc.getMailMerge().execute(dataTable);
        
            doc.updateFields();
            doc.save(getArtifactsDir() + "Field.MergeFieldImageDimension.docx");
        }
        
        /// <summary>
        /// Creates a data table with a single column
        /// </summary>
        private DataTable createDataTable(final String tableName, final String columnName, final String[] columnContents) {
            DataTable dataTable = new DataTable(tableName);
            dataTable.getColumns().add(new DataColumn(columnName));
        
            for (String s : columnContents) {
                DataRow dataRow = dataTable.newRow();
                dataRow.set(0, s);
                dataTable.getRows().add(dataRow);
            }
        
            return dataTable;
        }
        
        /// <summary>
        /// Sets the size of all mail merged images to one defined width and height
        /// </summary>
        private static class MergedImageResizer implements IFieldMergingCallback {
            public MergedImageResizer(final double imageWidth, final double imageHeight, final int unit) {
                mImageWidth = imageWidth;
                mImageHeight = imageHeight;
                mUnit = unit;
            }
        
            public void fieldMerging(final FieldMergingArgs args) {
                throw new UnsupportedOperationException();
            }
        
            public void imageFieldMerging(final ImageFieldMergingArgs args) {
                args.setImageFileName(args.getFieldValue().toString());
                args.setImageWidth(new MergeFieldImageDimension(mImageWidth, mUnit));
                args.setImageHeight(new MergeFieldImageDimension(mImageHeight, mUnit));
        
                Assert.assertEquals(mImageWidth, args.getImageWidth().getValue());
                Assert.assertEquals(mUnit, args.getImageWidth().getUnit());
                Assert.assertEquals(mImageHeight, args.getImageHeight().getValue());
                Assert.assertEquals(mUnit, args.getImageHeight().getUnit());
            }
        
            private double mImageWidth;
            private double mImageHeight;
            private int mUnit;
        }
      • MergeFieldImageDimension

        public MergeFieldImageDimension(double value, int unit)
        Creates an image dimension instance with the given value and the given unit. You should use a negative value to indicate that the original value of the corresponding image dimension should be applied.
        Parameters:
        value - The value.
        unit - A MergeFieldImageDimensionUnit value. The unit.

        Example:

        Shows how to set the dimensions of merged images.
        @Test
        public void mergeFieldImageDimension() throws Exception {
            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
            DataTable dataTable = createDataTable("Images", "ImageColumn",
                    new String[]
                            {
                                    getImageDir() + "Aspose.Words.gif",
                                    getImageDir() + "Watermark.png",
                                    getImageDir() + "dotnet-logo.png"
                            });
        
            doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(450.0, 200.0, MergeFieldImageDimensionUnit.POINT));
            doc.getMailMerge().execute(dataTable);
        
            doc.updateFields();
            doc.save(getArtifactsDir() + "Field.MergeFieldImageDimension.docx");
        }
        
        /// <summary>
        /// Creates a data table with a single column
        /// </summary>
        private DataTable createDataTable(final String tableName, final String columnName, final String[] columnContents) {
            DataTable dataTable = new DataTable(tableName);
            dataTable.getColumns().add(new DataColumn(columnName));
        
            for (String s : columnContents) {
                DataRow dataRow = dataTable.newRow();
                dataRow.set(0, s);
                dataTable.getRows().add(dataRow);
            }
        
            return dataTable;
        }
        
        /// <summary>
        /// Sets the size of all mail merged images to one defined width and height
        /// </summary>
        private static class MergedImageResizer implements IFieldMergingCallback {
            public MergedImageResizer(final double imageWidth, final double imageHeight, final int unit) {
                mImageWidth = imageWidth;
                mImageHeight = imageHeight;
                mUnit = unit;
            }
        
            public void fieldMerging(final FieldMergingArgs args) {
                throw new UnsupportedOperationException();
            }
        
            public void imageFieldMerging(final ImageFieldMergingArgs args) {
                args.setImageFileName(args.getFieldValue().toString());
                args.setImageWidth(new MergeFieldImageDimension(mImageWidth, mUnit));
                args.setImageHeight(new MergeFieldImageDimension(mImageHeight, mUnit));
        
                Assert.assertEquals(mImageWidth, args.getImageWidth().getValue());
                Assert.assertEquals(mUnit, args.getImageWidth().getUnit());
                Assert.assertEquals(mImageHeight, args.getImageHeight().getValue());
                Assert.assertEquals(mUnit, args.getImageHeight().getUnit());
            }
        
            private double mImageWidth;
            private double mImageHeight;
            private int mUnit;
        }
    • Property Getters/Setters Detail

      • getUnit/setUnit

        public int getUnit() / public void setUnit(int value)
        
        The unit. The value of the property is MergeFieldImageDimensionUnit integer constant.

        Example:

        Shows how to set the dimensions of merged images.
        @Test
        public void mergeFieldImageDimension() throws Exception {
            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
            DataTable dataTable = createDataTable("Images", "ImageColumn",
                    new String[]
                            {
                                    getImageDir() + "Aspose.Words.gif",
                                    getImageDir() + "Watermark.png",
                                    getImageDir() + "dotnet-logo.png"
                            });
        
            doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(450.0, 200.0, MergeFieldImageDimensionUnit.POINT));
            doc.getMailMerge().execute(dataTable);
        
            doc.updateFields();
            doc.save(getArtifactsDir() + "Field.MergeFieldImageDimension.docx");
        }
        
        /// <summary>
        /// Creates a data table with a single column
        /// </summary>
        private DataTable createDataTable(final String tableName, final String columnName, final String[] columnContents) {
            DataTable dataTable = new DataTable(tableName);
            dataTable.getColumns().add(new DataColumn(columnName));
        
            for (String s : columnContents) {
                DataRow dataRow = dataTable.newRow();
                dataRow.set(0, s);
                dataTable.getRows().add(dataRow);
            }
        
            return dataTable;
        }
        
        /// <summary>
        /// Sets the size of all mail merged images to one defined width and height
        /// </summary>
        private static class MergedImageResizer implements IFieldMergingCallback {
            public MergedImageResizer(final double imageWidth, final double imageHeight, final int unit) {
                mImageWidth = imageWidth;
                mImageHeight = imageHeight;
                mUnit = unit;
            }
        
            public void fieldMerging(final FieldMergingArgs args) {
                throw new UnsupportedOperationException();
            }
        
            public void imageFieldMerging(final ImageFieldMergingArgs args) {
                args.setImageFileName(args.getFieldValue().toString());
                args.setImageWidth(new MergeFieldImageDimension(mImageWidth, mUnit));
                args.setImageHeight(new MergeFieldImageDimension(mImageHeight, mUnit));
        
                Assert.assertEquals(mImageWidth, args.getImageWidth().getValue());
                Assert.assertEquals(mUnit, args.getImageWidth().getUnit());
                Assert.assertEquals(mImageHeight, args.getImageHeight().getValue());
                Assert.assertEquals(mUnit, args.getImageHeight().getUnit());
            }
        
            private double mImageWidth;
            private double mImageHeight;
            private int mUnit;
        }
      • getValue/setValue

        public double getValue() / public void setValue(double value)
        
        The value. You should use a negative value to indicate that the original value of the corresponding image dimension should be applied.

        Example:

        Shows how to set the dimensions of merged images.
        @Test
        public void mergeFieldImageDimension() throws Exception {
            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
            DataTable dataTable = createDataTable("Images", "ImageColumn",
                    new String[]
                            {
                                    getImageDir() + "Aspose.Words.gif",
                                    getImageDir() + "Watermark.png",
                                    getImageDir() + "dotnet-logo.png"
                            });
        
            doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(450.0, 200.0, MergeFieldImageDimensionUnit.POINT));
            doc.getMailMerge().execute(dataTable);
        
            doc.updateFields();
            doc.save(getArtifactsDir() + "Field.MergeFieldImageDimension.docx");
        }
        
        /// <summary>
        /// Creates a data table with a single column
        /// </summary>
        private DataTable createDataTable(final String tableName, final String columnName, final String[] columnContents) {
            DataTable dataTable = new DataTable(tableName);
            dataTable.getColumns().add(new DataColumn(columnName));
        
            for (String s : columnContents) {
                DataRow dataRow = dataTable.newRow();
                dataRow.set(0, s);
                dataTable.getRows().add(dataRow);
            }
        
            return dataTable;
        }
        
        /// <summary>
        /// Sets the size of all mail merged images to one defined width and height
        /// </summary>
        private static class MergedImageResizer implements IFieldMergingCallback {
            public MergedImageResizer(final double imageWidth, final double imageHeight, final int unit) {
                mImageWidth = imageWidth;
                mImageHeight = imageHeight;
                mUnit = unit;
            }
        
            public void fieldMerging(final FieldMergingArgs args) {
                throw new UnsupportedOperationException();
            }
        
            public void imageFieldMerging(final ImageFieldMergingArgs args) {
                args.setImageFileName(args.getFieldValue().toString());
                args.setImageWidth(new MergeFieldImageDimension(mImageWidth, mUnit));
                args.setImageHeight(new MergeFieldImageDimension(mImageHeight, mUnit));
        
                Assert.assertEquals(mImageWidth, args.getImageWidth().getValue());
                Assert.assertEquals(mUnit, args.getImageWidth().getUnit());
                Assert.assertEquals(mImageHeight, args.getImageHeight().getValue());
                Assert.assertEquals(mUnit, args.getImageHeight().getUnit());
            }
        
            private double mImageWidth;
            private double mImageHeight;
            private int mUnit;
        }