Hi,
I have various images that I need to put into a document header. I have 3. Two are the same size, and the other is quite large. There is potential for all the images in the future being different sizes.
When I insert the larger image into a word document header, it automamtically “shrinks to fit” the header margins.
I have tried the following code to try and replicate this, but this does not work (the image is still oversized and is not even in the header until I change it slightly)
// The best place for the watermark image is in the header or footer so it is shown on every page.
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
Shape shape = builder.insertImage(image);
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
// Calculate image left and top position so it appears in the centre of the page.
shape.setLeft((builder.getPageSetup().getPageWidth() - shape.getWidth()) / 2);
shape.setTop((builder.getPageSetup().getPageHeight() - shape.getHeight()) / 2);
doc.save(getMyDir() + "DocumentBuilder.InsertWatermark Out.doc");
I was only able to put the image into the header with a decent size by manually updating the height and width options of the image, which I don’t really want to be doing. The code I am doing with is as follows:
imageHeight = 120;
imageWidth = 450;
// set a header on a document. params: document to add image to and image URL
private Document setHeaderImage(Document doc, String imageUrl) throws Exception
{
DocumentBuilder builder = new DocumentBuilder(doc);
// Add the image to the document and set it's position and size
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
Shape shp = builder.insertImage("file:///"+imageUrl);
shp.setWrapType(WrapType.INLINE);
shp.setBehindText(true);
shp.setHeight(imageHeight);
shp.setWidth(imageWidth);
return doc;
}
Is there any way I can replicate the “shrink to fit” function when dealing with word directly?
Also, would making the image a “bufferedImage” make a difference?
Thanks.