How to set font in decimal number using HSSFFont - java

I want to set text font to 7.5 to a particular text in excel sheet. I am using below code to prepare the Font.
HSSFCellStyle txtStyle = (HSSFCellStyle)workbook.createCellStyle();
HSSFFont txtFont = (HSSFFont)workbook.createFont();
txtFont.setFontName("Arial");
txtFont.setFontHeightInPoints((short)7.5);
txtStyle.setFont(txtFont);
But it always takes font as 7 because of short type casting and hence targeted text in excel sheet is taking font 7 and not 7.5. I also tried method 'setFontHeight' but that also takes short as parameter. So is there any way by which I could set text font to decimal number?

Promoting comments to an answer - the POI HSSF Font class has two font size settings methods:
setFontHeight(short) - Set the font height in unit's of 1/20th of a point
setFontHeightInPoints(short) - Set the font height in point
Using setFontHeightInPoints is the easier one for most cases, and is recommended in the Javadocs. However, it'll only cope with whole-number font heights. That's most font sizes, but not all
To set a font height of 7.5, you'd need to change your code instead to be:
xtFont.setFontHeight((short)(7.5*20));
That uses the alternate one that takes 1/20 point sizes, so copes with non-integer values.

Related

Can not accurately set Excel column width Apache POI

This is my code (Java)
sheet.setColumnWidth(0, 100 * 256);
I expect the width of column 1 of the generated excel to be 100. But the actual width in excel is 99.22 . The search engine did not provide a reasonable explanation, can anyone help.
In Excel the column width 100 means that 100 default characters in default font and default font size fit into the cell. So how width 100 exactly is varies with default font and default font size. The default font and default font size is given by cell style Normal given in Styles group in Home tab.
In storage Excel stores the width having additional padding. So coulmn widt 100 is not exactly stored as 100 but as 100 + padding. But also the padding differs for different default fonts and default font sizes used.
Apache POI using XSSF (Office Open XML format) takes font Calibri and font size 11 as the defaults. Those are the defaults used by Excel too. So Units.DEFAULT_CHARACTER_WIDTH of Apache POI is default character width of that default font in size 11.
So using following column width setting:
...
int widthExcel = 100;
int width256 = (int)Math.round((widthExcel*Units.DEFAULT_CHARACTER_WIDTH+5f)/Units.DEFAULT_CHARACTER_WIDTH*256f);
sheet.setColumnWidth(0, width256);
...
should set column width exactly 100 in Excel if that Excel uses font Calibri and font size 11 as the defaults in cell style Normal.
Using other defaults the Units.DEFAULT_CHARACTER_WIDTH in the calculation must be replaced by other default character widths.
I modified my code(I got it from here https://blog.csdn.net/ab411919134/article/details/17606399):
sheet.setColumnWidth(0, (int) ((100 + 0.73) * 256));
The post mentioned that it should be 0.72, but 0.73 is correct for me
This excel column width meets expectations, but I still don’t know why

Calculate row height for HSSFRow

I'm writing an Excel (xls) sheet with Apache POI 3.13. I set the column width manually. If the content of a cell is too long, I want it to be wrapped and the column height to be adjusted.
If I set the wrapText property of the CellStyle to true, the text does not "flow" out of the cell any more, but how do I set the height of the row to a fitting value?
All approaches I've seen calculate the newline characters in the string. This is not working for me, since my text does not contain manual newlines.
This seems to be a LibreOffice bug, because with Excel on a Windows system it looks as expected.

iText Maximum Font Size

I'm using a fixed cell height to create a table.
If the font size is too large, the text is not visible in the table.
Is there a built-in function in iText that automatically reduces the font size to the maximum possible size, or do I have to implement this by myself?
Automatic font size is only possible in the context of AcroForm text fields. When you define the font size of a text field as 0, then a font size is chosen that fits the rectangle. In the case of a fixed cell height in a table, you are responsible to make sure that the text fits.
If you're concerned about the height, please take a look at the FitTextInRectangle example:
BaseFont bf = BaseFont.createFont();
int textHeightInGlyphSpace = bf.getAscent(text) - bf.getDescent(text);
float fontSize = 1000f * fixedHeight / textHeightInGlyphSpace;
This example was written in answer to Correct text position center in rectangle iText
If you're concerned about the width, then you need to use the getWidthPoint() method as explained here: How to calculate the string width in iText?
BaseFont bf = BaseFont.createFont();
float width = bf.getWidthPoint("My text", myFontSize);
You'll need to make sure that width doesn't exceed the width of the cell. To achieve this, you'll need to adjust myFontSize.
See my answer to this question: How to choose the optimal size for a font?

Font size in Jtextpane looks smaller then in other applications

I have JTextPane and if i write something into it with e.g. font size 12 then it looks smaller than text in e.g. MS Word with same size and same font family.
I've searched on the internet and I found that this is not just my specific problem. But I can't find any solution.
Set the bigger font size is not a solution for me.
Is it possible to change measure or something like that?
Actually there is DPI difference of java (72pixels per inch) and windows (96 pixels per inch). To reflect your fonts properly you can multiply them on the 96/72 on Windows.
You can override
public Font getFont(AttributeSet attr)
method of DefaultStyledDocument where retrieve font size from the attribute set and increase it with the proportion.
Actually, there is a trick for that. Through the Document of the JTextPane, you're able to specify the font-size of each character with setCharacterAttributes. Now imagine that you have some text with font-size 12 and then a text with font-size 14 inside your JTextPane. What happens if you edit the global font-size of the JTextPane using
myTextPane.setFont(myTextPane.getFont().deriveFont(newFontSize));
The answer is that your text will still be considered sized 12 and 14, but will be displayed bigger !
WARNING : I didn't try with a StyledDocument. Only with an HTMLDocument.

Resize font based on string length

From an xml file, I'm given a width, height and id. All of them can and do vary very quickly. Now, I'm asked to draw a rectangle using the width and height (an easy task), and place the id at its center. The id must not overflow out of the rectangle it's contained it.
For single-character strings, this is also easy - set the font size to the height, play a bit with the x position maybe, and it's centered. The problem is when it's multi-character strings.
So given a width and height and a string, how can you determine what font-size the string should appear in? Assume you have every bit of information you need on the rectangle you're drawing the string in.
[Edit]: I'm using the Graphics 2D class to draw everything.
Start with selecting a Font at your preferred (i.e. maximum) size.
Grab the FontRenderContext from your Graphics2D object using getFontRenderContext.
Use getStringBounds() on the Font to be rendered to get a Rectangle2D object for the specific String to be rendered. That object describes the final size of the String using that Font
Check if the size specified by that Rectangle2D is small enough.
4a. If it is small enough, you're done. Use the last Font you've checked.
4b. If it is too big, use Font.derive() to produce a smaller version of the Font and continue to use that and loop back to 3.
Don't quite have the time to give you a full working example, but here are a couple pointers that should get you going in the right direction. The graphics object you are using to draw with has a getFontMetrics() method, one of the methods on FontMetrics is stringWidth(String str) which gives you the width of a string in the current Font.
If the width is too big for your rectangle set the Font on the Graphics object to the same font just with a smaller size until it fits.
To horizontally center a string in a container (learned long ago in typing class in high school):
(rectangleWidth / 2) - (stringWidth / 2)
http://download.oracle.com/javase/1.5.0/docs/api/java/awt/FontMetrics.html
To create a Font with a smaller size, something like:
Font font = graphics.getFont();
Font smallerFont = font.derive(font.getSize() - 1);
graphics.setFont(smallerFont);
Hope this gets you going in the right direction.
I would recommend for this problem to remove as many unknowns as possible. In this case, the problem chiefly is that font characters can vary in width... well most. That's why I would use a good monospace font like courier new for the ID, that way you know what the width of each character is, you know the width of your rectangle and you know the number of characters in your string. You can simply reduce the pixel size of each character will till your string fits the available width.
Example, if the width of each character is 12px and you have 10 characters in your ID, then you need 120px to fit everything in. If you only have 80px available, it's simple math 80/10 = 8px font-size (reduce half a pixel for padding if you want.
Just my suggestion.

Categories

Resources