Crionics LOGO

TUTORIALS

 

 Printer Tutorial:
Printing PDF with JDK 1.3

As easy as 1, 2, 3...

 

Creating a job

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");

Printing

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();

Recommendations

Be very careful when using JDK 1.3:

  • The API doesn't provide any methods to wait for print completion. It is assumed that the print job is synchronous - which means that the process stops until the document is finished printing. However, some modern print drivers are not compatible - they will buffer the document in the internal printer cache and release the process. As a consequence, you may experience unexpected results when printing consecutive documents.
  • The Java Printjob API is very limited in features. It is highly recommended that you use the new version described in this document.


© 2000-2007 Crionics Inc. Report issues with this page