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.
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 |
One Reply to “Java printing of custom Swing Components”