Bug 51173

Summary: XSSFClientAnchor.setDx1() has no effect
Product: POI Reporter: at13
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED INVALID    
Severity: normal    
Priority: P2    
Version: 3.7-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   

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.