As easy as 1, 2, 3...
In this tutorial, we will show how to print a local PDF
document. Remember, there are really two different API used to print
when using Java. Here we describe the legacy API in use with
JDK earlier than version 1.4.
(Printing came at an early stage in the Java language life
cycle. Unfortunately, we had to wait until version 1.4 to see a decent
programming interface that allows complex print configurations.If you need to understand the new Print API better, you can skip this document and refer directly to this one.)
In order to print using JDK 1.3, a PrinterJob object is
required. This can easily be found by using the static method
getPrinterJob() from class java.awt.print.PrinterJob.
// Create a printer job:
PrinterJob myjob = PrinterJob.getPrinterJob();
You will also need a handle on a PDF document.
PdfDocument doc = new PdfDocument("mydocument.pdf");
The next step is to get the printer module. This module gives access to a set of easy-to-use methods to print document pages.
// Get the printer module:
PrinterModule pm = doc.getPrinterModule();
At this point things get easy, thanks to the jPDF intuitive API.
// Issue a print order. The process should block until the document is printed:
pm.print(myjob);
// We can close the document at this point to save resources:
doc.close();