Java PDF Blog

PDF solutions for big and small customers

Java and PDF development - our personal experiences and discoveries

Download JPedal

Download JPedal PDF viewers

PDF to Image service

Try our PDF to image conversion service now.

Java PDF Ebook Solution

Try our Ebook solution now.

Subscribe

Your email:

Java PDF blog

Current Articles | RSS Feed RSS Feed

Java printing of custom Swing Components

Posted by Mark Stephens on Wed, Feb 10, 2010 @ 09:50 AM
Submit to Digg digg it |  Add to delicious  delicious |  Submit to StumbleUpon StumbleUpon | Submit to Reddit reddit 

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.

 

 

0 Comments Click here to read/write comments

All Posts