Bug 42999 - HSSFPatriarch positioning problem
Summary: HSSFPatriarch positioning problem
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: unspecified
Hardware: PC Windows XP
: P5 minor with 4 votes (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords: APIBug
Depends on:
Blocks:
 
Reported: 2007-07-30 05:13 UTC by Bogdan Vasile
Modified: 2007-12-24 01:51 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
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