2

I've tried to print out a table using a dotmatrix printer, it worked but the text quality was very bad. So I tried to print it using the simple FileWriter:

FileWriter out;
try 
{
    out = new FileWriter("LPT1:");
    out.write(string);
    out.flush();
    out.close();
} 
catch (IOException ex) 
{
}

The problem is, I also want to print images and lines (to form a table). How do I do this without messing up the quality of the text.

William Wino
  • 3,599
  • 7
  • 38
  • 61

1 Answers1

3

Depending on the quality you expect, the most straight forward solution would be to use some ASCII pseudo graphics for the table.

column 1 | column 2 | column 3
______________________________
value 11 | value 12 | value 13
value 21 | value 22 | value 23
value 31 | value 32 | value 33

I case you expect to get solid lines for the table, you need to print everything in real graphics mode (instead of text mode of the printer). Therefore I would use JasperReports

edit A piece of code to show the principal of using ESC/P printer control codes to switch on/off the underline text printing mode.

final String UNDERLINE_ON = "\u001B\u002D\u0001";
final String UNDERLINE_OFF = "\u001B\u002D\u0000";
final String CRLF = "\r\n";

out.write(UNDERLINE_ON + "column 1 | column 2 | column 3" + UNDERLINE_OFF + CRLF);
out.write("value 11 | value 12 | value 13" + CRLF);
out.write("value 21 | value 22 | value 23" + CRLF);
out.write("value 31 | value 32 | value 33" + CRLF);

edit: The mentioned document about ESC/P codes can be accessed for example via

https://web.archive.org/web/20150213082718/http://support.epson.ru/products/manuals/000350/part1.pdf

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • I've tried to print the table AND the image using FlyingSaucer. It worked BUT the quality of the text was bad. That's why I switched to plain text printing in the first place. Underscore doesn't look good because it leaves unsymmetrical spaces between the text above and the text below, as you can see in your example. – William Wino Oct 16 '13 at 04:50
  • Have you tried to switch on the underline mode (ESC 1B 2D 1) for the table header? It wasn't possible to do show it correctly in my answer. The underscore should not be on a separate line. – SubOptimal Oct 16 '13 at 09:17
  • What do you mean underline mode? I've never heard of that, is that a printer specific function? – William Wino Oct 16 '13 at 09:37
  • Yes it's a special control code for the printer. [Wikipedia ESC/P](https://en.wikipedia.org/wiki/ESC/P) on the bottom is a link to a PDF which explains the mentioned code [Part 1: Introduction and Command Summary](http://support.epson.ru/products/manuals/000350/part1.pdf) (page 132). – SubOptimal Oct 16 '13 at 10:27
  • Interesting..You should update your answer. Should I send the "command string" along with the string using the FileWriter? So it would be like this : `out.write("column 1 | column 2 | column 3ESC 1B 2D 1");`? – William Wino Oct 16 '13 at 12:00
  • Can you explain a bit more about the `\u00..` what does that represent? An escape code? Sorry, I'm not familiar with this. – William Wino Oct 17 '13 at 09:38
  • I've read the manual, it says that I should use `ESC -` to turn underline on/off. And what does `1B 2D 1` mean? The hex code of `ESC -`? – William Wino Oct 17 '13 at 10:12
  • The hexadecimal values 1B 2D 01 are decimal values 27 45 1. 0x1B is the hexadecimal value for the ASCII control character ESC see [ASCII on Wikipedia](https://en.wikipedia.org/wiki/ASCII#ASCII_control_code_chart). See on page 132 in the manual under the point "Function" which explains the possible values for 'n'. So it means: 1B 2D 01 -> underline on, 1B 2D 00 -> underline off. – SubOptimal Oct 17 '13 at 10:54
  • \u001B means the Unicode code point 001B (which represents the ASCII control character ESC). For further information about Java and Unicode have a look here [Java Internationalization Tutorial - Unicode](http://docs.oracle.com/javase/tutorial/i18n/text/unicode.html) – SubOptimal Oct 17 '13 at 11:01
  • Perfect! You can add that to your answer as well I think. Thank you very much! – William Wino Oct 17 '13 at 11:37