You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Jim Talbut <jt...@spudsoft.co.uk> on 2011/09/27 12:14:50 UTC

Problems adding pictures to an XLSX sheet

Hi,

I'm adding three pictures to an XLSX sheet and the result is rather odd 
- the pictures all look correct in Excel, but when I try to move one of 
them (in Excel) it moves the wrong picture.
i.e. I drag Picture 3 to a new location and Picture 1 appears there at 
Pictures 3's size.

Is this something obvious that I'm doing wrong?

I can't post all my code, so I hope this is enough to show my (hopefully 
stupid) error:

     private Drawing getDrawing() {
         if( currentDrawing == null ) {
             currentDrawing = currentSheet.createDrawingPatriarch();
         }
         return currentDrawing;
     }

     private void placeImageInCurrentCell( int imageIdx, IImageContent 
image ) {
         System.err.println("Adding image " + imageIdx);
         Cell oldCell = currentCell;
         int widthNum = 0;
         if( currentCell == null ) {
             currentRow = this.currentSheet.createRow(rowNum);
             currentRow.setHeightInPoints( 
StyleManagerUtils.fontSizeInPoints( image.getHeight().toString() ) );

             // Allow image to span multiple columns
             int widthCm = (int)image.getWidth().convertTo( "cm" );
             int currentWidth = 0;
             for( widthNum = 0; currentWidth < ( widthCm * 1200 ); 
++widthNum ) {
                 currentWidth += currentSheet.getColumnWidth( widthNum );
             }
             System.err.println( "Calculated width num: " + widthNum );

             ++rowNum;
             currentCell = currentRow.createCell( 0 );
             currentCell.setCellType(Cell.CELL_TYPE_BLANK);
         } else {
             styleStack.mergeTop(image, ICellContent.class);
             widthNum = currentCell.getColumnIndex() + 1;
         }

         Drawing drawing = getDrawing();

         ClientAnchor anchor = wb.getCreationHelper().createClientAnchor();
         anchor.setCol1(currentCell.getColumnIndex());
         anchor.setRow1(currentCell.getRowIndex());
         anchor.setCol2(widthNum);
         anchor.setRow2(currentCell.getRowIndex() + 1);
         Picture pict = drawing.createPicture(anchor, imageIdx);

         //auto-size picture relative to its top-left corner
         // pict.resize();

         if( oldCell == null ) {
             CellStyle cellStyle = sm.getStyle(image);
             currentCell.setCellStyle(cellStyle);

             currentCell = null;
             currentRow = null;
         }
     }

And then, in another function:

                 int imageIdx = wb.addPicture( data, imageType );
                 placeImageInCurrentCell( imageIdx, image );

Thanks.

Jim

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Problems adding pictures to an XLSX sheet

Posted by Jim Talbut <jt...@spudsoft.co.uk>.
On 27/09/2011 21:52, Jim Talbut wrote:
> On 27/09/2011 11:14, Jim Talbut wrote:
>> Hi,
>>
>> I'm adding three pictures to an XLSX sheet and the result is rather 
>> odd - the pictures all look correct in Excel, but when I try to move 
>> one of them (in Excel) it moves the wrong picture.
>> i.e. I drag Picture 3 to a new location and Picture 1 appears there 
>> at Pictures 3's size.
>>
>> Is this something obvious that I'm doing wrong?
> Looking into this further I tried saving one copy of a sample XLSX 
> file from Excel and comparing that with a fresh one generated by POI 
> (in this isntance there are two pictures in the file).
> I think (without having much clue what I'm talking about) that the 
> problem lies with 
> /xdr:wsDr/xdr:twoCellAnchor/xdr:pic/xdr:nvPicPr/xdr:cNvPr/@id
>
> For the file corrected by Excel the two instances of 
> /xdr:wsDr/xdr:twoCellAnchor have different values for 
> /xdr:wsDr/xdr:twoCellAnchor/xdr:pic/xdr:nvPicPr/xdr:cNvPr/@id ("2" and 
> "3", if it matters what the actual values are), but for the POI file 
> they are both "1".
Ah, moving to v3.8-beta4 fixes this problem, but unfortunately I'm now 
affected by bug 51236 
(https://issues.apache.org/bugzilla/show_bug.cgi?id=51236).
If I can do anything to help someone work out a fix for that please let 
me know.

I guess I'll just have to special case my font colours until that gets 
fixed.

Jim



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Problems adding pictures to an XLSX sheet

Posted by Jim Talbut <jt...@spudsoft.co.uk>.
On 27/09/2011 11:14, Jim Talbut wrote:
> Hi,
>
> I'm adding three pictures to an XLSX sheet and the result is rather 
> odd - the pictures all look correct in Excel, but when I try to move 
> one of them (in Excel) it moves the wrong picture.
> i.e. I drag Picture 3 to a new location and Picture 1 appears there at 
> Pictures 3's size.
>
> Is this something obvious that I'm doing wrong?
Looking into this further I tried saving one copy of a sample XLSX file 
from Excel and comparing that with a fresh one generated by POI (in this 
isntance there are two pictures in the file).
I think (without having much clue what I'm talking about) that the 
problem lies with 
/xdr:wsDr/xdr:twoCellAnchor/xdr:pic/xdr:nvPicPr/xdr:cNvPr/@id

For the file corrected by Excel the two instances of 
/xdr:wsDr/xdr:twoCellAnchor have different values for 
/xdr:wsDr/xdr:twoCellAnchor/xdr:pic/xdr:nvPicPr/xdr:cNvPr/@id ("2" and 
"3", if it matters what the actual values are), but for the POI file 
they are both "1".

Jim



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org