Java Print API - Invalid space using Courier New monospace font

Let me first describe the picture below:

  • There are two printed articles. The only difference between the two is that several β€œout of paper” characters on the left are replaced by a dot β€œ.”. character in the document on the right.
  • The red line represents the left border to which the text should be aligned
  • The green curve represents my intention to align all the characters that it connects into one column. In fact, the green curve should be vertical.

enter image description here

I want all characters highlighted in green to be printed in one column.

The font of the string is Monospaced Courier New. However, whitespace does not seem to print as monospaced (see "Dotted" lines against lines with space characters at the beginning).

To print the line, I use the standard Java print service API on top of the JTextPane component:

PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintable(myTextPane); pj.print(); 

To my knowledge, the Java print service API actually calls the paint () methods of myTextPane. Therefore, the preview should look exactly like the print version of String.

However, it is not. The preview seems to misinterpret the characters of the monospace space (see last image). The preview looks exactly the way I want the text to print.

enter image description here

Any suggestions on how to get the JavaPrintServiceAPI to correctly print monospaced space characters?

+6
source share
1 answer

I do not think you can fix this in the print API. A.

First, divide each line after the green digit into the left and (possibly empty) right substring.

In JTextArea use align to justify a two-column HTML table, as described in How to Use HTML in Swing Components .

As an alternative to JTextPane use the GridLayout of JLabel . Give the left column RIGHT_ALIGNMENT and the right column LEFT_ALIGNMENT .

A two- JTable that uses JLabel for rendering may be the third alternative. See "Using Concept Tables: Editors and Renders" for details.

+1
source

Source: https://habr.com/ru/post/905204/


All Articles