MergeFieldImageDimension
Inheritance: java.lang.Object
All Implemented Interfaces: java.lang.Cloneable
public class MergeFieldImageDimension implements Cloneable
Represents an image dimension (i.e. the width or the height) used across a mail merge process.
To learn more, visit the Working with Fields documentation article.
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 getValue() / setValue(double) property.
Examples:
Shows how to set the dimensions of images as MERGEFIELDS accepts them during a mail merge.
public void mergeFieldImageDimension() throws Exception {
Document doc = new Document();
// Insert a MERGEFIELD that will accept images from a source during a mail merge. Use the field code to reference
// a column in the data source containing local system filenames of images we wish to use in the mail merge.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldMergeField field = (FieldMergeField) builder.insertField("MERGEFIELD Image:ImageColumn");
// The data source should have such a column named "ImageColumn".
Assert.assertEquals("Image:ImageColumn", field.getFieldName());
// Create a suitable data source.
DataTable dataTable = new DataTable("Images");
dataTable.getColumns().add(new DataColumn("ImageColumn"));
dataTable.getRows().add(getImageDir() + "Logo.jpg");
dataTable.getRows().add(getImageDir() + "Transparent background logo.png");
dataTable.getRows().add(getImageDir() + "Enhanced Windows MetaFile.emf");
// Configure a callback to modify the sizes of images at merge time, then execute the mail merge.
doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(200.0, 200.0, MergeFieldImageDimensionUnit.POINT));
doc.getMailMerge().execute(dataTable);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.MERGEFIELD.ImageDimension.docx");
}
///
/// Sets the size of all mail merged images to one defined width and height.
///
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 final double mImageWidth;
private final double mImageHeight;
private final int mUnit;
}
Constructors
Constructor | Description |
---|---|
MergeFieldImageDimension(double value) | Creates an image dimension instance with the given value in points. |
MergeFieldImageDimension(double value, int unit) | Initializes a new instance of this class. |
Methods
Method | Description |
---|---|
getUnit() | The unit. |
getValue() | The value. |
setUnit(int value) | The unit. |
setValue(double value) | The value. |
MergeFieldImageDimension(double value)
public MergeFieldImageDimension(double value)
Creates an image dimension instance with the given value in points.
Remarks:
You should use a negative value to indicate that the original value of the corresponding image dimension should be applied.
Examples:
Shows how to set the dimensions of images as MERGEFIELDS accepts them during a mail merge.
public void mergeFieldImageDimension() throws Exception {
Document doc = new Document();
// Insert a MERGEFIELD that will accept images from a source during a mail merge. Use the field code to reference
// a column in the data source containing local system filenames of images we wish to use in the mail merge.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldMergeField field = (FieldMergeField) builder.insertField("MERGEFIELD Image:ImageColumn");
// The data source should have such a column named "ImageColumn".
Assert.assertEquals("Image:ImageColumn", field.getFieldName());
// Create a suitable data source.
DataTable dataTable = new DataTable("Images");
dataTable.getColumns().add(new DataColumn("ImageColumn"));
dataTable.getRows().add(getImageDir() + "Logo.jpg");
dataTable.getRows().add(getImageDir() + "Transparent background logo.png");
dataTable.getRows().add(getImageDir() + "Enhanced Windows MetaFile.emf");
// Configure a callback to modify the sizes of images at merge time, then execute the mail merge.
doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(200.0, 200.0, MergeFieldImageDimensionUnit.POINT));
doc.getMailMerge().execute(dataTable);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.MERGEFIELD.ImageDimension.docx");
}
///
/// Sets the size of all mail merged images to one defined width and height.
///
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 final double mImageWidth;
private final double mImageHeight;
private final int mUnit;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | double | The value. |
MergeFieldImageDimension(double value, int unit)
public MergeFieldImageDimension(double value, int unit)
Initializes a new instance of this class.
Parameters:
Parameter | Type | Description |
---|---|---|
value | double | |
unit | int |
getUnit()
public int getUnit()
The unit.
Examples:
Shows how to set the dimensions of images as MERGEFIELDS accepts them during a mail merge.
public void mergeFieldImageDimension() throws Exception {
Document doc = new Document();
// Insert a MERGEFIELD that will accept images from a source during a mail merge. Use the field code to reference
// a column in the data source containing local system filenames of images we wish to use in the mail merge.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldMergeField field = (FieldMergeField) builder.insertField("MERGEFIELD Image:ImageColumn");
// The data source should have such a column named "ImageColumn".
Assert.assertEquals("Image:ImageColumn", field.getFieldName());
// Create a suitable data source.
DataTable dataTable = new DataTable("Images");
dataTable.getColumns().add(new DataColumn("ImageColumn"));
dataTable.getRows().add(getImageDir() + "Logo.jpg");
dataTable.getRows().add(getImageDir() + "Transparent background logo.png");
dataTable.getRows().add(getImageDir() + "Enhanced Windows MetaFile.emf");
// Configure a callback to modify the sizes of images at merge time, then execute the mail merge.
doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(200.0, 200.0, MergeFieldImageDimensionUnit.POINT));
doc.getMailMerge().execute(dataTable);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.MERGEFIELD.ImageDimension.docx");
}
///
/// Sets the size of all mail merged images to one defined width and height.
///
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 final double mImageWidth;
private final double mImageHeight;
private final int mUnit;
}
Returns: int - The corresponding int value. The returned value is one of MergeFieldImageDimensionUnit constants.
getValue()
public double getValue()
The value.
Remarks:
You should use a negative value to indicate that the original value of the corresponding image dimension should be applied.
Examples:
Shows how to set the dimensions of images as MERGEFIELDS accepts them during a mail merge.
public void mergeFieldImageDimension() throws Exception {
Document doc = new Document();
// Insert a MERGEFIELD that will accept images from a source during a mail merge. Use the field code to reference
// a column in the data source containing local system filenames of images we wish to use in the mail merge.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldMergeField field = (FieldMergeField) builder.insertField("MERGEFIELD Image:ImageColumn");
// The data source should have such a column named "ImageColumn".
Assert.assertEquals("Image:ImageColumn", field.getFieldName());
// Create a suitable data source.
DataTable dataTable = new DataTable("Images");
dataTable.getColumns().add(new DataColumn("ImageColumn"));
dataTable.getRows().add(getImageDir() + "Logo.jpg");
dataTable.getRows().add(getImageDir() + "Transparent background logo.png");
dataTable.getRows().add(getImageDir() + "Enhanced Windows MetaFile.emf");
// Configure a callback to modify the sizes of images at merge time, then execute the mail merge.
doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(200.0, 200.0, MergeFieldImageDimensionUnit.POINT));
doc.getMailMerge().execute(dataTable);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.MERGEFIELD.ImageDimension.docx");
}
///
/// Sets the size of all mail merged images to one defined width and height.
///
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 final double mImageWidth;
private final double mImageHeight;
private final int mUnit;
}
Returns: double - The corresponding double value.
setUnit(int value)
public void setUnit(int value)
The unit.
Examples:
Shows how to set the dimensions of images as MERGEFIELDS accepts them during a mail merge.
public void mergeFieldImageDimension() throws Exception {
Document doc = new Document();
// Insert a MERGEFIELD that will accept images from a source during a mail merge. Use the field code to reference
// a column in the data source containing local system filenames of images we wish to use in the mail merge.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldMergeField field = (FieldMergeField) builder.insertField("MERGEFIELD Image:ImageColumn");
// The data source should have such a column named "ImageColumn".
Assert.assertEquals("Image:ImageColumn", field.getFieldName());
// Create a suitable data source.
DataTable dataTable = new DataTable("Images");
dataTable.getColumns().add(new DataColumn("ImageColumn"));
dataTable.getRows().add(getImageDir() + "Logo.jpg");
dataTable.getRows().add(getImageDir() + "Transparent background logo.png");
dataTable.getRows().add(getImageDir() + "Enhanced Windows MetaFile.emf");
// Configure a callback to modify the sizes of images at merge time, then execute the mail merge.
doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(200.0, 200.0, MergeFieldImageDimensionUnit.POINT));
doc.getMailMerge().execute(dataTable);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.MERGEFIELD.ImageDimension.docx");
}
///
/// Sets the size of all mail merged images to one defined width and height.
///
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 final double mImageWidth;
private final double mImageHeight;
private final int mUnit;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. The value must be one of MergeFieldImageDimensionUnit constants. |
setValue(double value)
public void setValue(double value)
The value.
Remarks:
You should use a negative value to indicate that the original value of the corresponding image dimension should be applied.
Examples:
Shows how to set the dimensions of images as MERGEFIELDS accepts them during a mail merge.
public void mergeFieldImageDimension() throws Exception {
Document doc = new Document();
// Insert a MERGEFIELD that will accept images from a source during a mail merge. Use the field code to reference
// a column in the data source containing local system filenames of images we wish to use in the mail merge.
DocumentBuilder builder = new DocumentBuilder(doc);
FieldMergeField field = (FieldMergeField) builder.insertField("MERGEFIELD Image:ImageColumn");
// The data source should have such a column named "ImageColumn".
Assert.assertEquals("Image:ImageColumn", field.getFieldName());
// Create a suitable data source.
DataTable dataTable = new DataTable("Images");
dataTable.getColumns().add(new DataColumn("ImageColumn"));
dataTable.getRows().add(getImageDir() + "Logo.jpg");
dataTable.getRows().add(getImageDir() + "Transparent background logo.png");
dataTable.getRows().add(getImageDir() + "Enhanced Windows MetaFile.emf");
// Configure a callback to modify the sizes of images at merge time, then execute the mail merge.
doc.getMailMerge().setFieldMergingCallback(new MergedImageResizer(200.0, 200.0, MergeFieldImageDimensionUnit.POINT));
doc.getMailMerge().execute(dataTable);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.MERGEFIELD.ImageDimension.docx");
}
///
/// Sets the size of all mail merged images to one defined width and height.
///
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 final double mImageWidth;
private final double mImageHeight;
private final int mUnit;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | double | The corresponding double value. |