Bug 42999

Summary: HSSFPatriarch positioning problem
Product: POI Reporter: Bogdan Vasile <virussu_ro>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: minor Keywords: APIBug
Priority: P5    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

Description Bogdan Vasile 2007-07-30 05:13:14 UTC
Hi folks!

I have a problem positioning a picture inside a cell range. I use a code similar
to this one:

wb = new HSSFWorkbook();
sheet = wb.createSheet("report");
patriarch = sheet.createDrawingPatriarch();
loadPicture(picturePath);//custom method			
picData = getPictureBytes();
indexPicture=wb.addPicture( picData, HSSFWorkbook.PICTURE_TYPE_JPEG );
		
anchor = new HSSFClientAnchor( 500 , 0 , 300 , 255 ,(short)0,1,(short)1,3);

anchor.setAnchorType( 0 );
patriarch.createPicture(anchor, indexPicture);

And then I save the workbook.

The problem is that when the anchor is created, the positioning is bad if the
dx1 coordinate (500) is greater than the dx2 coordinate (300). The program will
actualy put the patriarch having the start coordinate in the first cell equal to
300, and the end coordinate in the last cell equal to 500. 

So it looks like if dx1>dx2, the two coordinates will switch between them.

I had some hard time trying to get around this, but I did not find a way. I also
tried to create a new patriarch with the default constructor, and then to set
each coordinate, row and column, but the result stays the same.

Should I use some other way to add a picture?

Thanks in advance,
Bogdan
Comment 1 Yegor Kozlov 2007-07-30 06:11:07 UTC
Hi,

The bug is in org.apache.poi.hssf.model.ConvertAnchor.
This object is responsible for converting HSSF anchors to the
corresponding low-level escher record.

There is an extra check which cause dx1 ans dx2 to be swapped if dx1>dx2:
...
anchor.setDx1( (short) Math.min(a.getDx1(), a.getDx2()) );
anchor.setDy1( (short) Math.min(a.getDy1(), a.getDy2()) );
...

It does make sense for child anchors - you can't have dx2>dx1 in a child anchor.
For client anchors it is extra and can be safely removed. I will commit the
patch shortly.

Regards,
Yegor
Comment 2 Yegor Kozlov 2007-08-03 11:09:48 UTC
Fixed
Comment 3 Yaroslav Rozhylo 2007-12-06 07:57:24 UTC
The same problem in AnchorHeight calculations in 
org.apache.poi.hssf.usermodel.HSSFClientAnchor

public float getAnchorHeightInPoints(HSSFSheet sheet )
{
int y1 = Math.min( getDy1(), getDy2() );
int y2 = Math.max( getDy1(), getDy2() );
int row1 = Math.min( getRow1(), getRow2() );
int row2 = Math.max( getRow1(), getRow2() );
...

Please, fix it in 3.0.2 release.
Comment 4 Yegor Kozlov 2007-12-06 08:27:52 UTC
OK. The fix will go in 3.0.2 beta2.

Yegor
Comment 5 Yegor Kozlov 2007-12-24 01:51:52 UTC
Fixed

Yegor