We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Images into headers

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.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

does anyone know if the "auto resize" is even possible or do I have to size manually.

Many thanks

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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 = ImageIO.read(imgFile);

//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;

}

else

{

//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));

}

else

{

targetHeight = (targetWidth * (ratioWidth / ratioHeight));

}

}

//Add the image to the document and set it's position and size

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);

Shape shp = builder.insertImage(img, targetWidth, targetHeight);

shp.setWrapType(WrapType.INLINE);

shp.setBehindText(true);

doc.save("C:\\Temp\\out.doc");

Hope this helps.

Best regards.

Thanks very much Alexy, I will try this out.

regards

Kevin