Hello!
Please see the image properties in Microsoft Word. Its width and height are 100% each. Maybe the image is too large originally. You can reduce its size after you create a Shape object.
Regards,
Thanks for your request. Maybe you would like to fit an image to a page size. You can try calculating 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.
If this does not help, please attach a document that will show me what the expected output is.
Best regards.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.