Hallo
I want to dynamically include Images in the documents. The images should never exceed some limit in size. The data source is an automatically generated xml file.
I put an «image(80pt;50pt):ImagePath» in my template and an implementation of the IFieldMergingCallback interface.
class ShapeSetFieldMergingCallback implements IFieldMergingCallback {
public void fieldMerging(FieldMergingArgs args) throws Exception {
// Implementation is not required.
}
public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
Shape shape = new Shape(args.getDocument(), ShapeType.IMAGE);
shape.setAspectRatioLocked(true);
String imageFileName = args.getFieldValue().toString();
shape.getImageData().setImage(imageFileName);
double imageWidth = shape.getImageData().getImageSize().getWidthPoints();
double imageHeight = shape.getImageData().getImageSize().getHeightPoints();
double targetWidth = args.getImageWidth().getValue();
targetWidth = (targetWidth<0)? imageWidth:targetWidth;
double targetHeight = args.getImageHeight().getValue();
targetHeight = (targetHeight<0)? imageWidth:targetHeight;
if (targetHeight / imageHeight > targetWidth / imageWidth) {
targetHeight = imageHeight * targetWidth / imageWidth;
} else {
targetWidth = imageWidth * targetHeight / imageHeight;
}
shape.setWidth(targetWidth);
shape.setHeight(targetHeight);
shape.setWrapType(WrapType.SQUARE);
shape.setAllowOverlap(false);
args.setShape(shape);
}
}
But now the images overlap my footer. This seems rather complicated for such a simple task.
I just want the normal behaviour of image fields with the images keeping the aspect ratio.
Best,
Peter