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
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
Fixed
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.
OK. The fix will go in 3.0.2 beta2. Yegor
Fixed Yegor