Set image height with row height in excel using apache poi

This may be a stupid question, but I can’t find a solution. How to set the line height depending on the image height? Here is part of my code:

int pictureIdx = workBook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
CreationHelper helper = workBook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(0);
anchor.setRow1(i);
anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE);
HSSFPicture pict = (HSSFPicture) drawing.createPicture(anchor, pictureIdx);
Dimension size = pict.getImageDimension();
double scaledWidth = size.getWidth();
double procentage = (1070.0d * 100d) / scaledWidth;
double autosize = procentage / 100.0d;
pict.resize(autosize);
short h = (short) (pict.getImageDimension().getWidth());
row.setHeight(h);

in Excel, the height of my image is much greater than the line height

+4
source share
1 answer

It is doubtful that you will need this in a year, but for what it's worth, you assign the width of the images as the height of the line.

short h = (short) (pict.getImageDimension().getWidth());

Also setHeight uses twip as a unit, which is 1/20 of a point. Therefore, you need to multiply the h value by more than 20 times to get into the pixel range.

+2
source

Source: https://habr.com/ru/post/1528937/


All Articles