Bug 51173 - XSSFClientAnchor.setDx1() has no effect

本文探讨了Apache POI中XSSFClientAnchor类的方法setDx1等在定位图片时存在的问题及解决方案。通过调整参数单位为像素并乘以XSSFShape.EMU_PER_PIXEL常量,实现了图片的精确位置设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/hantiannan/article/details/7012363 

 

 

Bug 51173 -  XSSFClientAnchor.setDx1() has no effect
Status:RESOLVED INVALID
 
Product:POI
Component:XSSF
Version:3.7-FINAL
Hardware:PC All
 
Importance:P2 normal (vote)
Target Milestone:---
Assigned To:POI Developers List
 
URL: 
Keywords: 
 
Depends on: 
Blocks: 
 Show dependency tree
 
Reported:2011-05-09 08:20 UTC by at13
Modified:2014-04-02 10:15 UTC (History)
CC List:0 users
 
 
 


Attachments

NoteYou need to log in before you can comment on or make changes to this bug.
 
Description at13  2011-05-09 08:20:45 UTC
The methods:
setDx1(...)
setDx2(...)   
setDy1(...)
setDy2(...)

have no effect on XSSFClientAnchor.
On HSSFClientAnchor you can position/size a picture within the cell and it works, on XSSFClientAnchor it does not.
I also tried the newest 3.8-beta2 and it also does not work.
Comment 1 Nick Burch  2011-05-09 15:58:41 UTC
Is the problem that the XML isn't changing, or that the expected effect in excel isn't seen? (These are two very different problems)
Comment 2 at13  2011-05-10 08:14:54 UTC
Hello!
I created two xlsx files one with calling the setdx... methods and one without. Then I opened these files as zip files and compared the sheet1.xml files in the folder xl/worksheets. Both of the sheet1.xml-files were exactly the same.
So if I did everything correct, it seems that the methods have no effect on the creating of the XML. I am not very familiar with the internal structure of the xslx files.
Comment 3 at13  2011-05-11 06:38:35 UTC
OK, these methods DOES have an effect. But the parameter values have to be much bigger in XSSFClientAnchor than in HSSFClientAnchor.
I have not find out yet, how to calculate these values or what unit they have. 
The JavaDoc only says e.g. for setDx1(int dx1):
"dx1 the x coordinate within the first cell"
but not what unit the parameter has.
Is there any documentation for that?
So I think there is no bug, so this bug entry can be closed?
Comment 4 Nick Burch  2011-05-11 10:00:02 UTC
Could you try creating a few files with HSSF with various dx/dy values, then open these in Excel, save as .xlsx, and see what the values were written as? That might give us a hint on the scaling factor different between the two formats
Comment 5 at13  2011-05-11 13:53:55 UTC
The secret is solved!
You have to calculate everything in pixels, the anchor-width and -height, the image-width and -height and with that the margin values (dx and dy).
In the setDx.../setDy... methods you have to multiply the dx/dy pixel-values with the constant XSSFShape.EMU_PER_PIXEL and voila, everything works!
Pleeeeeaaaase write JavaDocs for those secret things! I like the apache libraries and APIs very much and you guys make a great job, but documentation is very important for using the libraries.
Comment 6 Nick Burch  2011-05-13 09:24:32 UTC
Note added in r1102624, thanks for the investigations!
Comment 7 suman saurav  2013-07-24 11:18:05 UTC
Hi Nick,

I have same issue here. And I have tried every EMU value like XSSFShape.EMU_PER_PIXEL and multiplied it by 10 and 20, but it doesn't make an effect in converted sheet. Images are still coming at the edge of cell.

Below is the code, i am using write now:

XSSFClientAnchor anchor = new XSSFClientAnchor(XSSFShape.EMU_PER_PIXEL*200,XSSFShape.EMU_PER_PIXEL*200,0,0,(short)col,row,(short)col1,row1);

Please suggest, if i did anything wrong.

Thanks,
Saurav
Comment 8 Malcolm McMahon  2014-04-02 10:15:01 UTC
(In reply to suman saurav from comment #7)
> Hi Nick,
> 
> I have same issue here. And I have tried every EMU value like
> XSSFShape.EMU_PER_PIXEL and multiplied it by 10 and 20, but it doesn't make
> an effect in converted sheet. Images are still coming at the edge of cell.
> 
> Below is the code, i am using write now:
> 
> XSSFClientAnchor anchor = new
> XSSFClientAnchor(XSSFShape.EMU_PER_PIXEL*200,XSSFShape.EMU_PER_PIXEL*200,0,0,
> (short)col,row,(short)col1,row1);
> 
> Please suggest, if i did anything wrong.
> 
> Thanks,
> Saurav


The real problem behind all these bug report seems to be in XSSFPicture.resize();

In here we find

 anchor.setCol2(col2);

 anchor.setDx1(0);  // !!


 anchor.setDx2(pref.getDx2());


 anchor.setRow2(row2);

 anchor.setDy1(0);  // !!

In other words calling resize discards any offsets you've set in your anchor.

I've replaced the resize call with:

Picture pic = drawing.createPicture(anchor, pictures.get(value));
ClientAnchor pref = pic.getPreferredSize();
anchor.setDx2(pref.getDx2() + anchor.getDx1());
anchor.setDy2(pref.getDy2() + anchor.getDy1());

And it works as expected. Something slightly more complicated would be needed if the picture extended over more than one cell.


 anchor.setDy2(pref.getDy2());

This kills any offsets you set, and if you don't call resize your picture will probably collapse.

The resize source could be changed easily enough to preserver dx1 and dy1, and I don't imagine any existing code would break.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值