TUTORIALS
Printer Tutorial:
Printing PDF using JDK 1.4 or better
As easy as 1, 2, 3...
This tutorial shows you how to use jPDF Printer to print PDF documents using the Java Print API (JDK 1.4+).
Please take some time to read the formal Java Print Service documents at http://java.sun.com/javase/6/docs/technotes/guides/jps/index.html
Discovering print services
The new Print API bundled with JDK 1.4 (and better) uses the notion of services. One of its greatest features is its ability to discover print services at runtime. We offer a handy sample application in our jPDF distribution to discover all the print services available in your system. The sample shows their names along with all their attributes.
In fact, you can find the default print service by using the following line of code. Typically, this is your default printer.
// Find the default printer:
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
However, you may want to select a different printer programmatically. This can be done easily by using the following code:
// If you know the printer name:
PrintService service = getPrinter("myPrinterName");
if (service == null)
{
System.err.println("There are no printer selected");
return;
}
While we don't discuss the topic in this tutorial, you may also use advanced lookup functions to find Postscript or text printers. Please refer to the specialised documentation for further information.
Once you find the print service, you can create a job. The print job is one of the objects required to start a PDF rendering.
// Create the print job:
DocPrintJob job = service.createPrintJob();
Using attributes
Attributes provide a flexible way to select printer features. Features - such as the print quality, the paper orientation, and the number of copies - can be set programmatically.
It is important to note that not all features may be available for a given printer. Please refer to our handy sample application to identify what features are available. (The source code is available in the samples folder of the jPDF distribution.)
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
// You may want to add one or more print attributes; make sure your
// printer supports them:
//
attributes.add(new Copies(2));
attributes.add(Sides.DUPLEX);
attributes.add(PrintQuality.HIGH);
attributes.add(SheetCollate.COLLATED);
attributes.add(OrientationRequested.LANDSCAPE);
attributes.add(MediaSize.getMediaSizeForName(MediaSizeName.ISO_A4));
Printing
Now comes the easy part - opening the document, getting the module and starting the print request.
// Open the document:
PdfDocument doc = new PdfDocument("mydoc.pdf");
// Get the printer module:
PrinterModule pm = doc.getPrinterModule();
// Monitor print job events for completion, see point #4:
PrintJobWatcher pjDone = new PrintJobWatcher(job);
// Print it:
pm.print(job, attributes);
Waiting for completion
Print requests are not required to be synchronous. Indeed, they might run in the background. This depends on the printer driver and how your operating system handles the print pool.
Using JDK 1.4, however, it is possible to get notified when a print job completes. For this purpose, we provide an easy-to-use class called PrintJobWatcher. Its source code is bundled in the jPDF distribution.
Using this class, waiting for a print job to complete is as simple as the following code fragment:
// Wait for the print job to complete:
pjDone.waitForDone();
// It is now safe to close the document:
doc.close();
|