Font size in Jtextpane looks smaller then in other applications - java

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.

Related

Jasper Print page different from the contens in the screen

our Java project has a problem and I need your advice:
We have a JTextPane component which is customized to support pagination. The component's content must be printed using JasperReport. The problem is: although we have used the same font's name, size, style, and page's height and width for the JTextPane and JasperReport, but the print-page is always different from the contents in the screen, typically a few characters are not aligned properly between the 2 devices (print-page and screen-page); as a result the content may be displayed in only 1 screen-page but will be printed into 2 pages.
EDIT:
There is a JTextarea (First text) with the same Font as the Jasper print (Second text) (DejaVu Serif 10), but the length of the texts are different.
The question is: I need the same text length on the screen and on the Jasper Print, but i dont know how to do that. For example I have a JTextarea with a fix width of pixel maybe 700. The jasper textfield has also a width of 700 pixel. Now I expect the text on the screen and on jasper ist exactly the same if I configure the same font, but there are different. What can I do to solve this problem?

How to set font in decimal number using HSSFFont

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.

how to make JTextPane the size of a piece of paper?

OK, So i finally figured out how to print a JTextPane, and really have a fully functioning document editor.
Anyway, Can I make the JTextPane the size of a piece of paper (with page breaks) So you can easily predict the appearance of the document you are typing? Any tips/hints would be helpful!
edit: format of the JTextPane is 'text/rtf', if that helps.
May be the links could help
http://java-sl.com/Pagination_In_JEditorPane.html
http://java-sl.com/Pagination_In_JEditorPane_Print.html
The Printable returned by the getPrintable() method of JTextComponent "formats the document content appropriately for the page size." If this is insufficient, please edit your question to include an sscce that exhibits the problem you're having.
I would suggest using a layout manager
How can I create a JTextArea with a specified width and the smallest possible height required to display all the text?
As for setting the width, you'll want to calculate it based on the page format specified. It could be 8.5x11 or in landscape or a completely different size altogether.
pageFormat.getImageableWidth();
pageFormat.getImageableHeight();

Setting the width of the characters of a font to be uniform in java

Good Day! I am currently working on a program that prints a string in the printer. I am having problems regarding the display of the string to be printed. Ideally I want the font width of every character to be uniform. The problem occurs when when other language characters are set to be displayed. I've noticed that japanese characters are larger in width than the normal characters. Can I set these japanese characters to follow the font width of the normal characters?
Example:
NNNNN
NPD事本
Notice that the string with japanese encoding is larger in width. How can I make this string to follow the font width of my designated font? Is there a way? or is my case hopeless? Thanks in advance.
There are such things as monospace fonts, where each character takes the same width. I do not know if Japanese has such a font, or if there are English fonts and Japanese fonts that are both monospace and have the same width in pixels.
https://stackoverflow.com/questions/586503/complete-monospaced-unicode-font
The above question was closed as "off-topic" but it has some good links. The general consensus seemed to be you are either hosed, or in for a nightmare of working with different fonts in the same document.

Size Mismatch Between Swing Fonts and Printable Fonts in Java

I've got a panel displaying a JTextPane backed by a StyledDocument. When I print a string of text in, say Arial 16, the text it prints is the same size as the Arial 16 Word prints. However, the Arial 16 in the JTextPane appears to be smaller than the Arial 16 Word displays. Is there some sort of flaw in the translation of Swing fonts to Windows system fonts or something of the sort that makes it difficult (or impossible) to print accurately?
I can achieve an approximation by scaling down the size of my font before printing, but this never quite gets me the results I would like, as it's not possible in all cases to reproduce things like equivalent numbers of words on a line, etc.
Has anybody run into this before?

Categories

Resources