Jasper Print page different from the contens in the screen - java

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?

Related

PDFBox: Differentiating between transparent and non-transparent text

I have a task where I have to extract text which are behind images and have been OCR-ed from the image itself. This text is transparent. The problem is there is an image which has text behind it which is not OCR-ed, it is just normal text and it is not transparent. How can I differentiate between the needed (transparent) and the not-needed (non-transparent) text?
Here is a representative pdf file: https://easyupload.io/rbo333
Image OCR text should be extracted on page 2,3,12 but text is also extracted on page 4. On page 4 there is no OCR text behind images, but there is regular text under the image. I need to somehow filter that out as I only need OCR text.
So the images have in front of them or behind them transparent text. I thought that meant that they have no color, but #mkl said that they might have colors, but they are empty glyphs. The pdf specification also states that they can have color even if they are transparent. To be truly transparent the characters need to be rendered with neither stroking, nor non-stroking colors.
There is a RenderingMode enum in PDFBox, or Fontbox for exactly this purpose and its NEITHER value denotes whether something is transparent. I could extract it with the help of this answer.
The solution code looks like this.
#Override
protected void processTextPosition(TextPosition character) {
characterRenderingModes.put(character, getGraphicsState().getTextState().getRenderingMode());
super.processTextPosition(character);
}
This is an overriden method of the PDFTextStripper class and it goes through every character on the page/s and gets their RenderingModes. After that when needed I get the RenderingModes out of the map based on the characters I needed to examine.

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.

Issue with TextArea height

I had posted the same question in oracle javafx forum too but haven't got a response. So trying my luck here.
I have a requirement where in the content of the text area is dynamically populated from the database. I am able to successfully retrieve and display the data on the text area.
However when the content is too large, I am not able to dynamically set the height of the text area. When I try to display the same as a label, the display is flawless, dynamically sets the height as per the content. So, I tried to create a label, with same content and dynamically bind the height to the preferred height as below, but it doesn't work.
// Generate User Note Description
TextArea textArea = new TextArea();
Label text = new Label();
// SETTING THE TEXT TO A LABEL TO RETRIEVE THE HEIGHT
text.setText(usrNotes.getNote().trim());
// ALWAYS DISPLAYS 0.0
System.out.println("height::"+text.getHeight());
if (isMyNote) {
// ALWAYS SETS TO THE MINIMUM HEIGHT OF 60.0
textArea.setText(usrNotes.getNote().trim());
textArea.setPrefWidth(Screen.getPrimary().getVisualBounds().getWidth() - 500.0);
textArea.setWrapText(true);
textArea.setMinHeight(60.0);
// WITHOUT THIS BINDING, DISPLAYS LOT OF EXTRA SPACE AFTER THE TEXT
textArea.prefHeightProperty().bind(text.heightProperty());
textArea.setStyle("-fx-padding:0 5 2 1; -fx-font-size: 1.1em;-fx-background-color:white");
} else if (!isMyNote) {
// THIS IS PERFECT, AS EXPECTED SETS THE HEIGHT DYNAMICALLY
text.setText(usrNotes.getNote().trim());
text.setStyle("-fx-padding:0 5 2 1;");
text.setStyle("-fx-border-color: white;-fx-font-size: 1.1em;-fx-background-color:#F5F5F5;");
text.setWrapText(true);
text.setPrefWidth(Screen.getPrimary().getVisualBounds().getWidth() - 500.0);
text.setMinHeight(60.0);
}
I would highly appreciate if someone can provide a hint on how to resolve this issue.
Thanks -SV
The reason text.getHeight() returns 0, (and therefore have to bind to text.heightProperty()) is because the height isn't calculated when the component is constructed. It is calculated when the component is rendered to the screen.
If you want to calculate a height ahead of time, I believe you will have to use something like FontMetrics (http://docs.oracle.com/javase/tutorial/2d/text/measuringtext.html) to calculate the width and line height of your string, break up the string into tokens to figure out where line breaks will fall (based on your width), and then figure out how many lines you will need (and therefore, how high your TextArea needs to be).

How to fill out horizontal PDF forms using the PDFBox

How does one fill out a horizontal pdf form with the PDFBox library?
I access my fields and fill them using the supplied example code and it works fine. But, if the pdf page is tilted horizontally the filled out text are still left in the vertical position.
I have tried rotating the page first and then filling the form but the fields seem to be independent. I have also tried formatting the field through the various set methods defined for PDField and PDTextbox but this has no effect either.
Finally, I know that some of the rotation properties are controlled through the PDAnnotation and PDAnnotationWidget but trying to set their PDAppearanceCharacteristics has no effect on the initial text rotation. Rather, a user is required to interact with the field in order for this to take effect.
Thanking in advance,
J3lly

How to show all the content of Jtable in netbeans

Using netbeans I was able to show the content of my database using Jtable, but the problem is that when I run the file I got the Jtable but not all of the content of the indivdual record appear entirely. Because some columns has a paragraph entry not a few words
You'd have to be able to know the number of lines & the text height used by the jtable. From there you can set the row height (globally for the table, or for individual rows).
You would then need a table cell renderer capable of displaying multi line content.
Depending on your requirements, you'd might be better of displaying a one line summary of the cell & allow for tooltips to display the full content (depending on the size of te content) or a popup window with a none editable text component
UPDATE
Sorry for the short comment, I was on my IPad.
You have two choices, depending on what state your UI is in. You can grab a reference to the FontMatrics from the JTables graphics context. This will only work if the JTable has been realised (rendered on the screen)
FontMetrics fm = myTable.getFontMetrcis(myTable.getFont());
int height = fm.getHeight();
This example, of course, assumes you are using the same font as the JTable. If not, you'll need to supply the correct font.
Or, if the UI hasn't been realised yet, you'll need to construct a compatible image a extract the font metrics from it. This is a little more complicated as it begins to deal with the graphics configuration and devices...
BufferedImage img = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(1, 1, Transparency.TRANSLUCENT);
Graphics2D g2d = img.createGraphics();
FontMetrics fm = g2d.getFontMetrics(font);
int height = fm.getHeight();
g2d.dispose();
Once you have the height of the font, you should be able to calculate the height of the text, assuming the text is broken up into lines (or you can split the lines your self). Now if that's not the case (or you want to provide you own word/line wrapping), this become increasingly complicated.
You can check here http://docs.oracle.com/javase/tutorial/2d/text/drawmulstring.html for how to render text using graphics2D for hints (you can use this to split text into groups as you need)
You might also want to check out http://www.jroller.com/santhosh/entry/multiline_in_table_cell_editing1 which shows a great idea for a multi line editor.

Categories

Resources