Java printing of custom Swing Components

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

to

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

decode_pdf.print();

becomes

printJob.print(doc,null);

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) {
System.out.println(“printDataTransferCompleted=”+printJobEvent.toString());
}

public void printJobCompleted(PrintJobEvent printJobEvent) {
System.out.println(“printJobCompleted=”+printJobEvent.toString());
}

public void printJobFailed(PrintJobEvent printJobEvent) {
System.out.println(“printJobEvent=”+printJobEvent.toString());
}

public void printJobCanceled(PrintJobEvent printJobEvent) {
System.out.println(“printJobFailed=”+printJobEvent.toString());
}

public void printJobNoMoreEvents(PrintJobEvent printJobEvent) {
System.out.println(“printJobNoMoreEvents=”+printJobEvent.toString());
}

public void printJobRequiresAttention(PrintJobEvent printJobEvent) {
System.out.println(“printJobRequiresAttention=”+printJobEvent.toString());
}
}

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.

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

One thought on “Java printing of custom Swing Components

  1. […] to convert this to a DocPrintJob, i followed this link which told me how to convert from PrintJob to DocPrintJob. So now my code became […]

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>