Thanks Yegor.
I have to insert images precisely enough, so I try to use java.awt.* to
calculate the column width. The dpi is fixed at 96 on Windows, but the
default font may be changed. So I wrote the code as follows:
public final class HssfHelper
{
private final static int DPI_WINDOWS_NORMAL = 96;
private final static int DPI_INDUSTRY_STANDARD = 72;
/**
* Get default font width in points
*
*/
public static int getDefaultFontWidth(HSSFWorkbook workbook) {
//get default workbook font. HSSFWorkbook.getFontAt(0) should always
return the default one.
HSSFFont defFont = workbook.getFontAt((short)0);
//cast workbook font to Java font
Font font = new Font(defFont.getFontName(), Font.PLAIN,
defFont.getFontHeightInPoints());
//get font metrics
FontMetrics fontMetrics = new Label().getFontMetrics(font);
//get char width
fontWidth = fontMetrics.charWidth('0');
fontWidth = fontWidth * DPI_WINDOWS_NORMAL / DPI_INDUSTRY_STANDARD;
//Normally, Windows font is larger than industry standard
return fontWidth;
}
/**
* Get column width in points
*
*/
public static int getColumnWidthInPoints(HSSFWorkbook workbook, HSSFSheet
sheet, int colIndex) {
return (int)(sheet.getColumnWidth(colIndex) *
getDefaultFontWidth(workbook) + 255) / 256;
}
}
But this still only works with default font settings. Is there something
wrong of my code? Thanks for any help.
George
Yegor Kozlov wrote:
>
> I hope my belated answer is of help
>
> HSSFPicture.resize() as well as XSSFPicture.resize() indeed work only for
> the default font which is Arial 10pt for .xls
> and Calibri 11pt for .xlsx.
>
> Excel uses a funny coordinate system to position graphic objects. X and Y
> axes are measured not in pixels but in units
> proportional to a character width of the default font. To properly
> position and size an image POI needs to translate the
> actual dimensions measured in pixels to columns and rows. This is the key
> of the problem. At the moment POI performs
> this translation using hardcoded constants obtained empirically for Arial
> 10pt and Calibri 11pt. It works OK in most
> cases and also explains why HSSFPicture.resize() is 'off' if the default
> font is changed.
>
> Unfortunately it can not be easily fixed. To measure a custom font POI
> needs to load it (i.e. the font must be
> physically available) and get the font metrics using java.awt.* utilities.
> It will make the result machine dependent and
> impossible to test.
>
> I'm going to update javadocs on HSSFPicture.resize() and close Bug 48415
> as 'wontfix'
>
> Yegor
>
>> Dear all,
>>
>> As my test result, this function only works when the default font size of
>> workbook not changed. If I modified its size (For example, from 12 to 22)
>> and then call this function to insert an image, it stretchs horizontally.
>>
>> Image 1 (Default font size set to 12):
>> http://old.nabble.com/file/p26738706/1.jpg
>>
>> Image 2 (Default font size set to 22):
>> http://old.nabble.com/file/p26738706/2.jpg
>>
>> I've checked the source code of HSSFPicture.java, function resize() calls
>> another function getPixelWidth() to calculate the column width in pixels,
>> but the later function only works correctly when the default font size of
>> workbook not changed.
>>
>> Can somebody give me some suggestions to correct this problem? Thanks.
>>
>> George
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
>
--
View this message in context:
http://old.nabble.com/Bug-of-HSSFPicture.resize%28%29-tp26738706p26909776.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]