Image size in docx file

Aspose.words for Java:

How to insert a image into Document with its exact original size?

I use the following codes, but the image in the docx file was displayed in 40% rather than 100%.

DocumentBuilder docxBuilder = new DocumentBuilder(doc);
docxBuilder.moveToDocumentEnd();
// docxBuilder.insertImage(imageFileName);
Shape image = new Shape(doc, ShapeType.IMAGE);
image.setWrapType(WrapType.INLINE);
image.getImageData().setImage(imageFileName);
// image.setWidth(image.getImageData().getImageSize().getWidthPixels())
docxBuilder.insertNode(image);

Hi

Thanks for your inquiry. Please try using DocumentBuilder.InsertImage method:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/insertimage/
Hope this helps.
Best regards.

Hi,
I attached image and generated docx files.

documentBuilder.insertImage(imageFileName);

still image is not displayed for 100%.

Please help.

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,

Hi

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.