Java POI 3.2 图像高度/宽度控制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/759856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POI 3.2 Image Height/Width controlling
提问by
Using POI version 3.2
使用 POI 版本 3.2
Issue: Not able to resize an image to its original height and width. I am able to add an image to the excel file.
问题:无法将图像大小调整为其原始高度和宽度。我可以将图像添加到 excel 文件中。
After adding image I call picture.resize(); method.
添加图像后,我调用 picture.resize(); 方法。
Later I resize the columns of the excel file by calling sheet.setColumnWidth(columnindex, columnwidth) the image losses its original height/width.
后来我通过调用 sheet.setColumnWidth(columnindex, columnwidth) 调整excel文件的列大小,图像丢失了其原始高度/宽度。
Please help.
请帮忙。
回答by jwl
Shouldn't you just hold onto the picture object and call resize() very last, after your setcolumnwidth()s are done?
你不应该在你的 setcolumnwidth()s 完成后,抓住图片对象并最后调用 resize() 吗?
回答by John kendall
I found that you must NOT call picture.resize()
until after sheet.autoSizeColumn(i)
even if you are not calling autoSizeColumn(i)
on the column to which the picture is anchored.
我发现即使您没有调用图片所锚定的列,也不能在此picture.resize()
之后sheet.autoSizeColumn(i)
调用autoSizeColumn(i)
。
回答by flob
On a HSSFWorkbook with a custom font I did the following to get a logo to be displayed in the right dimensions:
在带有自定义字体的 HSSFWorkbook 上,我执行以下操作以使徽标以正确的尺寸显示:
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setDx1(LOGO_MARGIN * XSSFShape.EMU_PER_PIXEL);
anchor.setDx2(LOGO_WIDTH_IN_FUNNY_POI_FORMAT * XSSFShape.EMU_PER_PIXEL);
anchor.setDy1(LOGO_MARGIN * XSSFShape.EMU_PER_PIXEL);
anchor.setDy2(LOGO_HEIGHT_IN_FUNNY_POI_FORMAT * XSSFShape.EMU_PER_PIXEL);
drawing.createPicture(anchor, pictureIndex);
Where I set LOGO_HEIGHT... and LOGO_WIDTH.. to the wanted pixel size of the image.
我将 LOGO_HEIGHT... 和 LOGO_WIDTH.. 设置为图像所需的像素大小。
The resulting image was not at its original ratio and not the expected pixel size. So I used the expected size / current size ratio and adjusted LOGO_WIDTH.. and LOGO_HEIGHT.. accordingly. Not beautiful, but it works :/
生成的图像不是原始比例,也不是预期的像素大小。所以我使用了预期的尺寸/当前尺寸比率并相应地调整了 LOGO_WIDTH.. 和 LOGO_HEIGHT..。不漂亮,但它有效:/
And don't call Picture.resize()
afterwards.
Picture.resize()
之后不要打电话。
See Apache POIU Bug 52504or this newsgroup discussion about poi Picture.resize()
and POI quick guide for inserting pictures.
请参阅Apache POIU Bug 52504或有关 poiPicture.resize()
和POI 插入图片快速指南的新闻组讨论。
Update: current code looks like this, the original image is 2000x450:
更新:当前代码如下所示,原始图像为 2000x450:
LOGO_MARGIN = 2;
int index = getLogoPictureIndex();
CreationHelper helper = this.wb.getCreationHelper();
Drawing drawing = this.sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setDx1(LOGO_MARGIN * XSSFShape.EMU_PER_PIXEL);
anchor.setDy1(LOGO_MARGIN * XSSFShape.EMU_PER_PIXEL);
Picture pic = drawing.createPicture(anchor, index);
pic.resize(0.064);
回答by Piotr
Good solution is to use resize() function with scale argument:
好的解决方案是使用带有 scale 参数的 resize() 函数:
double scale = 0.3;
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize(scale);