Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit image height with row height in excel using apache poi

Maybe it is stupid question, but i can not find solution How can i set row height depending on 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 my image height is much bigger than row height

like image 889
slavov Avatar asked Oct 21 '22 13:10

slavov


1 Answers

Doubt you would still need this after one year but for what its worth, you are assigning the pictures width as row height.

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

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

like image 126
Frank Avatar answered Oct 23 '22 03:10

Frank