[prev in list] [next in list] [prev in thread] [next in thread] 

List:       poi-user
Subject:    Re: Computing anchor size for easy image insertion?
From:       Yegor Kozlov <yegor () dinom ! ru>
Date:       2007-02-19 15:48:53
Message-ID: 842163343.20070219184853 () dinom ! ru
[Download RAW message or body]

There isn't an easier way do scale images.

There was a discussion recently about scaling.
Excel measures images in units of 1/256th of a character width of the
default font. Conversion from these "weird" units to pixels is not
trivial. It's the main problem.

I have ideas how to add such support. Check POI in a month or two.
There is a chance I will commit it.

Yegor

ÒÁV> Hi all,

ÒÁV> I'm using Jakarta POI 3.0-alpha3 to write an Excel spreadsheet with
ÒÁV> images and I'm having some trouble computing the HSSFClientAnchor
ÒÁV> size.

ÒÁV> At the moment I try to manage it by adding column widths and row
ÒÁV> heights until I reach approximately the size of the image in cells,
ÒÁV> and then set up dx and dy, but I can almost never get it to work
ÒÁV> perfectly and images are never exactly at 100%.

ÒÁV> Is there an easier way to do this? I have searched around in google,
ÒÁV> groups and google code search but couldn't find anything useful. How
ÒÁV> do you people do this?

ÒÁV> Thanks a lot!

ÒÁV> Below is a snippet of my code. Basically, you give the starting row/
ÒÁV> col position of the image you want to insert and it goes around adding
ÒÁV> row/column sizes into imgHeight and imgWidth until their values are
ÒÁV> equal to or greater than the size of the image. If they're greater, it
ÒÁV> substracts one row or one column, depending on what was greater, and
ÒÁV> puts the remaining pixels on dx or dy.

ÒÁV>         protected int addImage(HSSFWorkbook wb, HSSFSheet sheet, InputStream
ÒÁV> img, int numRow, short numCol) {
ÒÁV>                 int x1 = 0;
ÒÁV>                 int y1 = 0;
ÒÁV>                 short x2 = 0;
ÒÁV>                 int y2 = 0;
ÒÁV>                 int row1 = numRow;
ÒÁV>                 short col1 = numCol;
ÒÁV>                 int row2 = numRow;
ÒÁV>                 short col2 = numCol;
ÒÁV>                 ImageData d = new ImageData();
ÒÁV>                 int idx = loadAndConvertPicture(img,wb,d);
ÒÁV>                 int imgHeight = 0;
ÒÁV>                 int imgWidth = 0;

ÒÁV>                 while(imgWidth <d.getWidth() || imgHeight <d.getHeight()) {
ÒÁV>                         if(imgWidth <d.getWidth()) col2++;
ÒÁV>                         if(imgHeight <d.getHeight()) row2++;
ÒÁV>                         if(imgWidth != d.getWidth()) {
ÒÁV>                                 int colWidth = (int)
ÒÁV> (sheet.getColumnWidth((short)col2)/48);
ÒÁV>                                 if(imgWidth+colWidth<=d.getAmplada()) {
ÒÁV>                                         imgWidth +=colWidth;
ÒÁV>                                 } else {
ÒÁV>                                         col2--;
ÒÁV>                                         x2=(short)(d.getWidth()-imgWidth);
ÒÁV>                                         imgWidth=d.getWidth();
ÒÁV>                                 }
ÒÁV>                         }
ÒÁV>                         if(imgHeight != d.getHeight()) {
ÒÁV>                                 int rowHeight;
ÒÁV>                                 HSSFRow row = sheet.getRow(row2);
ÒÁV>                                 if(row != null) rowHeight = (int)
ÒÁV> row.getHeight()/20;
ÒÁV>                                 else rowHeight = (int)
ÒÁV> sheet.getDefaultRowHeight()/20;
ÒÁV>                                 if(row == null) row = sheet.createRow(row2);
ÒÁV>                                 if(imgHeight +rowHeight<=d.getHeight()) {
ÒÁV>                                         imgHeight +=rowHeight;
ÒÁV>                                 } else {
ÒÁV>                                         row2--;
ÒÁV>                                         y2=d.getHeight()-imgHeight ;
ÒÁV>                                         imgHeight =d.getHeight();
ÒÁV>                                 }
ÒÁV>                         }
ÒÁV>                 }

ÒÁV> (note: variable and custom method names are translated since I'm not
ÒÁV> coding this in English; there might be a bug in there somewhere due to
ÒÁV> this translation. My apologies if this is the case.)

ÒÁV> ---------------------------------------------------------------------
ÒÁV> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
ÒÁV> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
ÒÁV> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic