Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

Java printing of custom Swing Components

1 min read

If you are implementing printing in Java, there is a really good tutorial showing how to add printing support to any Swing component. Printing generally works very well and we use it in our PDF library to provide printing of PDF files.

The example uses a PrinterJob to print via Java – essentially, your Swing component just needs to implement Pageable or Printable and have a print() method. This is often virtually identical to the draw method, but just renders onto a Print Graphics2D object rather than the Screen Graphics2D object.

However, PrinterJob is a rather basic function and does not have all the features of DocPrint. In particular, DocPrintJob offers a couple of very useful additional features:-

1. It can print different sized pages (with PrinterJob it seems hard-coded to the size of the first page).

2. It adds Listeners functionality to allow monitoring of Print activity. The method addPrintJobListener provides events to query the printer.

Converting from PrinterJob to DocPrintJob

It is very straight-forward to switch from using PrinterJob to DocPrintJob. In this example I have a custom Swing component decode_pdf which implements Pageable to print itself. Here are the steps:-

1. Alter your PrinterJob object to a DocPrintJob. In my code I just had to change

PrinterJob pj = PrinterJob.getPrinterJob();


PrintService[] service=PrinterJob.lookupPrintServices(); //list of printers

printJob= service[i].createPrintJob(); //i is whichever printer you use

2. Wrap your Pageable or Printable object inside a doc, specifying which interface to use in the call

//wrap in Doc as we can then add a listeners
Doc doc=new SimpleDoc(decode_pdf, DocFlavor.SERVICE_FORMATTED.PAGEABLE,null);

3. Alter the print call from directly calling your Swing component to use the doc so




Finally, you can now add a listener to track what happens.

printJob.addPrintJobListener(new PDFPrintJobListener());
Here is a simple example of the Listener class

private class PDFPrintJobListener implements PrintJobListener {
public void printDataTransferCompleted(PrintJobEvent printJobEvent) {

public void printJobCompleted(PrintJobEvent printJobEvent) {

public void printJobFailed(PrintJobEvent printJobEvent) {

public void printJobCanceled(PrintJobEvent printJobEvent) {

public void printJobNoMoreEvents(PrintJobEvent printJobEvent) {

public void printJobRequiresAttention(PrintJobEvent printJobEvent) {

So switching to DocPrintJob is simple and easy and improves printing support. And if you use our PDF library for printing, you should see the benefits in the next release.

This post is part of our “Printing Articles Index” in these articles, we aim to help you understand printing in Java and PDF’s.

Are you a Developer working with PDF files?

Our developers guide contains a large number of technical posts to help you understand the PDF file Format.

Do you need to solve any of these problems?

Display PDF documents in a Web app
Use PDF Forms in a web browser
Convert PDF Documents to an image
Work with PDF Documents in Java
Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

One Reply to “Java printing of custom Swing Components”

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2022. All rights reserved.