J2Printer14PS Test Document
Wildcrest Associates

http://www.wildcrest.com
March 29, 2007
 


 
Abstract
This document is designed for testing the J2PrinterWorks Java 2 printing component from Wildcrest Associates. In particular, it will illustrate the use of the print-to-Postscript capability of the J2PrinterWorks J2Printer14 printing class.

Postscript printing
Although J2PrinterWorks normally prints documents to actual printers, it can also print the equivalent document directly to a Postscript file. The resulting file can be viewed or printed later using Adobe Acrobat, Ghostscript, or other utilities for dumping Postscript files to Postscript printers. When printed, the resulting documents should look identical to the document J2PrinterWorks would have printed directly to the printer.

J2Printer14 method that performs the print-to-Postscript  function is:
The fileName can either be relative to the application working directory or may be specified using an absolute path. If fileName is null or the empty string, the file name will be assumed to be out.ps. If fileName has no extension, the extension .ps will be added to the file name.

Test program buttons
The J2Printer14PS test program provides three buttons to control its various functions:
Button Function
Read .HTML file Opens a file dialog, read .html file into display window.
Print Preview Opens the standard J2PrinterWorks print preview dialog.
Print to PS Calls the method printToPS("out.ps").


Required JDK releases
The J2PrinterWorks print-to-Postscript capability uses the Java PrintService classes introduced in the JDK 1.4 release. As a result, the printToPS() method is only available under the J2Printer14 subclass, and programs will not run on Java runtimes prior to JDK 1.4.

Sample code
The following code sample illustrates how easy it is to use J2PrinterWorks to print any Pageable document to either a Postscript or PDF file:
J2Printer14 printer = new J2Printer14();
J2TextPrinter textPrinter = new J2TextPrinter(yourJTextPane);
printer.addPageable(textPrinter);
printer.setLeftMargin(.25);
printer.setRightMargin(.25);
printer.setTopMargin(.25);
printer.setBottomMargin(.25);
printer.printToPS("printout.ps");

Note in the code sample above, the four margins are set explicitly. Since, unlike regular printers, the Postscript target has no physical margin limitations, so it can print right up to the edge of any page. As a result, the default margins for printToPS printing are 0.0 on all four sides. Thus, if you want to make your document look more like a conventional printed page, set some non-zero margins as shown above.

Additional considerations
If you require a PDF file as output, J2PrinterWorks includes a method printToPDF for this purpose. Using the printToPDF method requires the use of the free, open source component iText.jar available from http://www.lowagie.com/iText. Alternatively, you can use the printToPS method to print to Postscript, then use programs such as Adobe Acrobat Distiller or Ghostscript (PS2PDF) to convert your .ps file to a .pdf. In our experience, .ps files are about twice the size of the equivalent .pdf file.

The J2PrinterWorks printToPDF method honors the current setting of setSeparatePrintThread(boolean), just like the regular J2Printer print method. Therefore, by default, the printToPDF call returns immediately and the actual printToPDF operations are carried out asynchronously in a separate thread. If you wish to have printToPDF execute from your calling thread and not return until printing to the file is complete, you should call setSeparatePrintThread(false) in advance of your call to printToPDF. The full set of J2PrinterWorks print progress events are fired during the execution of printToPDF, so that if printToPDF executes asynchronously, your UI thread can receive print progress events and provide a progress report to the user.