For the most printing clients, is extremely useful to know if a print job has finished or failed.

The Java Print Service API provide some functionalities to get informed about these scenarios. All we have to do is:

When the print job state changes, we will be notified. We can do anything is needed, for example:

In the example bellow, we will log every print job status change:

import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;

public class LoggerPrintJobListener implements PrintJobListener {

    // Your favorite Logger class goes here!
    private static final Logger LOG = Logger.getLogger(LoggerPrintJobListener.class);
public void printDataTransferCompleted(PrintJobEvent pje) {
    LOG.info("Print data transfer completed ;) ");
}

public void printJobCompleted(PrintJobEvent pje) {
    LOG.info("Print job completed =) ");
}

public void printJobFailed(PrintJobEvent pje) {
    LOG.info("Print job failed =( ");
}

public void printJobCanceled(PrintJobEvent pje) {
    LOG.info("Print job canceled :| ");
}

public void printJobNoMoreEvents(PrintJobEvent pje) {
    LOG.info("No more events to the job ");
}

public void printJobRequiresAttention(PrintJobEvent pje) {
    LOG.info("Print job requires attention :O ");
}
}

Finally, we can add our print job listener implementation on the print job before the print request itself, as follows:

DocPrintJob printJob = printService.createPrintJob();

printJob.addPrintJobListener(new LoggerPrintJobListener());

printJob.print(doc, pras);

The PrintJobEvent pje argument

Notice that every method has a PrintJobEvent pje argument. We don’t use it in this example for simplicity purposes, but you can use it to explore the status. For example:

pje.getPrintJob().getAttributes();

Will return a PrintJobAttributeSet object instance and you can run them in a for-each way.