Images into headers

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.
Shape shape = builder.insertImage(image);
// 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); + "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
    Shape shp = builder.insertImage("file:///"+imageUrl);
    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?

does anyone know if the “auto resize” is even possible or do I have to size manually.
Many thanks

Thanks for your request. You can calculate size of image. For example you can try using the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Read image from file to BufferedImage
File imgFile = new File("C:\\Temp\\test.jpg");
BufferedImage img =;
// Calculate width and height of the page
PageSetup ps = builder.getCurrentSection().getPageSetup();
Double targetHeight = ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin();
Double targetWidth = ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin();
// Get size of an image
Double imgHeight = ConvertUtil.pixelToPoint(img.getHeight());
Double imgWidth = ConvertUtil.pixelToPoint(img.getWidth());
if (imgHeight < targetHeight && imgWidth < targetWidth)
    targetHeight = imgHeight;
    targetWidth = imgWidth;
    // Calculate size of an image in the document
    Double ratioWidth = (Double)(imgWidth / targetWidth);
    Double ratioHeight = (Double)(imgHeight / targetHeight);
    if (ratioWidth > ratioHeight)
        targetHeight = (targetHeight * (ratioHeight / ratioWidth));
        targetHeight = (targetWidth * (ratioWidth / ratioHeight));
// Add the image to the document and set it's position and size
Shape shp = builder.insertImage(img, targetWidth, targetHeight);

Hope this helps.
Best regards.

Thanks very much Alexy, I will try this out.