The Java platform [TM] provides a printing api which can be used for printing text and graphics. Java printing api always prints in graphic mode which can result in very slow printing if you use a dot matrix printer. TextPrinter is a java package developed to overcome this problem.
When to use TextPrinter:
TextPrinter does not uses the printing service of the operating system. In other words, you will not see the printing job in the windows printer monitor. TextPrinter sends the reports directly to the parallel port the printer is connected to. This is done either using the java commapi package or by writting into the an operating system file ( "PRN:" for windows).
TextPrinter does not only send the text you want to print but it also sends the appropiate printer commands for using bold and italic fonts, selecting font types, size of the font, character sets etc...
Note that each printer family will use a different set of commands for this reason you must check if your printer is supported. Some of the supported printer are:
You can also use the so called "Plain" printer, but in this case you will not be able to use italic and bold fonts, or any other feature other than printing text.
The product and the examples can be executed by just unzipping the downloaded file into an empty directory and running the appropiate BAT file.
If you need compile a program which uses TextPrinter you must add the rtextpr.jar file to your classpath.
If you want to use the commapi for accessing the printer (optional) you must
download the Java
Communications API. You must download the package and follow the directions
in the file PlatformSpecific.html. For exampe, if you are running java on windows
you must:
The following tutorial provides an introduction to the main classes of the package. For a complete list of all available classes and method please read the javadocs.
PrinterFactory
The PrinterFactory class is used to get a TextPrinter implementation for the type of printer you specify:
TextPrinter printer=PrinterFactory.getPrinter(printertype);
where printertype can be:
- IBM-PROPRINTER
- EPSON-ESCP2
- EPSON-ESCP
- IBM-PPDS
- HP-PCL3
- HP-PCL5
- DIABLO
- PLAIN (default if any other value passed)
PrinterPort
The PrinterPort interface is used to physically access the printer. There are 2 classes implementing the PrinterPort interface:
- FilePort: writes into operating system file which represent the parallel port. For example "PRN:" in windows or "/dev/lpr" for Linux. If can also write to a network printer using the syntax "//computername/printername". Example of use:
FilePort port=new FilePort("PRN:");
- HDParallelPort: access the printer using the commapi package (see installation). This can only access local printers and requires the installation od the commapi however it read printer responses like "error", "out of paper" or printer "on-line". Example of use:
HDParallelPort port=new HDParallelPort("LPT1");
The PrinterPort class is passed to the TextPrinter.startJob() method as described in the next section.
TextPrinter
The TextPrinter object returned by the PrinterFactory is the core of the printing process. It must be used in the following way:
// create the properties of your job
JobProperties job= printer.getDefaultJobProperties();
// start job with given properties on given port
printer.startJob(port,job);
// print your text here
// end printing
printer.endJob();You need to call the startJob() method providing the port object (see previous section) and the properties you request for your job. The JobProperties include properties like paper size and format.
The printing finishes when you execute endJob().Printing of you text can be performed in 2 manner:
- You can specify the absolute possition of the text you want to print. For example:
prop=printer.getDefaultTextProperties();
printer.printString("Hello",10,0,prop); // print at row 10, columns 0
printer.printString(....)
....
// print page when done
printer.newPage();
- or you can continuosly print:
prop=printer.getDefaultTextProperties();
printer.printString("Hello",prop);
printer.newLine();
printer.printString("Hello",prop);
printer.newLine();
......
The TextProperties class is used to set the appearance of your text (font, size ....).
JobProperties
The JobProperties class is used in the startJob method to some global properties. The most important are:
- Size of the page (JobProperties.rows and JobProperties.cols properties).
- Margins of the page (JobProperties.topMargin, JobProperties.bottomMargin, JobProperties.leftMargin and JobProperties.right Margin properties).
- Page orientation ( JobProperties.landscape). Note that not all printers support landscape printing.
TextProperties
Whenever you print a text you must pass a TextProperties object which is used to specify the following properties:
- Style of the font (TextProperties.italic , bold, underlined , subscript , superscript, doubleStrike and doubleWide). Note that not all printers will support all styles.
- Font type (TextProperties.fontName). The names of the fonts depend on the printer.
- Font size (TextProperties.pitch). Possible values are 10 , 12 and 15 characters per inch.
- Character set (TextProperties.characterSet). The names of the character sets depend on the printer.
The following example can be used to test the capabilities of your printer:
TextPrinter printer=PrinterFactory.getPrinter("HP-PCL3"); // use the correct printer type here
JobProperties job= printer.getDefaultJobProperties();
printer.startJob(port,job);
// plain style
TextProperties prop=printer.getDefaultTextProperties();
printer.printString("This must be NORMAL",prop);
printer.newLine();// bold style
prop=printer.getDefaultTextProperties();
prop.bold=true;
printer.printString("This must be BOLD",prop);
printer.newLine();// italic style
prop=printer.getDefaultTextProperties();
prop.italic=true;
printer.printString("This must be ITALIC",prop);
printer.newLine();// underlined
prop=printer.getDefaultTextProperties();
prop.underlined=true;
printer.printString("This must be UNDERLINED",prop);
printer.newLine();// double wide
prop=printer.getDefaultTextProperties();
prop.doubleWide=true;
printer.printString("This must be double wide",prop);
printer.newLine();// double strike
prop=printer.getDefaultTextProperties();
prop.doubleStrike=true;
printer.printString("This must be double strike",prop);
printer.newLine();// subscript
prop=printer.getDefaultTextProperties();
prop.subscript=true;
printer.printString("This must be subscript",prop);
printer.newLine();// superscript
prop=printer.getDefaultTextProperties();
prop.superscript=true;
printer.printString("This must be superscript",prop);
printer.newLine();printer.endJob();
The following tables show the features supported by each printer:
Epson-ESC P and P2
|
HP-PCL3 and 5
|
IBM-Proprinter
|
|
Bold |
Yes
|
Yes
|
Yes
|
Italic |
Yes
|
Yes
|
|
Underlined |
Yes
|
Yes
|
Yes
|
Font types |
Roman,
SansSerif, Courier , Prestige, Script , OCR-B, OCR-A , Orator, Orator-S
|
Univers,
LinePrinter, CG Times, Courier, Albertus Antique Olive, Clarendon, Coronet,
Garamond Antiqua, Letter Gothic, Marigold, CG Omega, Arial, Times New
Roman, Symbol, Wingdings
|
|
Character sets |
437
(USA), ISO-IR-69 (France), ISO-IR-21 (Germany), ISO-IR-4 (England), Denmark
(Denmark), Sweden (Sweden), ISO-IR-15 (Italy), ISO-IR-17 (Spain), Japan
(Japan), ISO-IR-60 (Norway), Denmark II (Denmark II), ISO-IR-85 (Spain
II), Latin America (Latin America), Korea (Korea), 932 (Japanese), 850
(Multilingual), 851 (Greek), 853 (Turkish), 855 (Cyrillic), 860 (Portugal),
863 (Canada-French), 865 (Norway), 852 (East Europe), 857 (Turkish II),
862 (Hebrew), 864 (Arabic), 866 (Russian), ISO8859-7 (Latin/Greek), ISO8859-1
(Latin 1), ISO8859-2 (Latin 2), 862 (Iceland)
|
ISO-IR-60
(Norwegian 1), ISO-IR-61 (Norwegian 2), ISO-IR-4 (United Kingdom), ISO-IR-69
(French), ISO-IR-21 (German), ISO-IR-15 (Italian), ISO-IR-14 (JIS ASCII),
ISO-IR-57 (Chinese), ISO-8859-1 (Latin 1), ISO-IR-11 (Swedish), ISO-IR-17
(Spanish 1), ISO-IR-10 (Swedish 2), ISO-IR-16 (Portuguese 1), IISO-IR-84
(Portuguese 2), ISO-IR-85 (Spanish 2), ISO-IR-6 (ASCII) ISO-IR-2 (IRV),
ROMAN8, 1252 (Windows), PC-8 (437 USA default), 850
|
437
(USA), 850 (Multilingual), 860 (863), 865 (Norway), 858, ISO-IR-6 (ASCII),
ISO-IR-69 (French), ISO-IR-21 (German), ISO-IR-4 (United Kingdom), ISO-IR-17
(Spanish 1), ISO-IR-15 (Italian), ISO-IR-17 (Spanish 1), ISO-IR-60 (Norwegian
1), ISO-IR-85 (Spanish 2)
|
Double strike |
Yes
|
Yes
|
|
Double wide |
Yes
|
Yes
|
|
Condensed fonts |
Yes
|
||
Size (chars per inch) |
10,
12, 15
|
10,
12, 15
|
10,
12
|
Superscript / Subscript |
Yes
|
Yes
|
|
Landscape |
Yes
|
||
Quality draft |
Yes
|
||
Line interscape |
6,
8
|
1,2,3,4,6,8,12,16,24,48
|
8,12,18,24
|
IBM-PPDS
|
Diablo
|
Plain
|
|
Bold |
Yes
|
Yes
|
|
Italic | |||
Underlined |
Yes
|
Yes
|
|
Font types | |||
Character sets | |||
Double strike |
Yes
|
||
Double wide |
Yes
|
||
Condensed fonts | |||
Size (chars per inch) |
10,
12 , 17.1
|
||
Superscript / Subscript |
Yes
|
||
Landscape |
Yes
|
||
Quality draft | |||
Line interscape |
8,12,18,24
|
Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries