Listening for Print Job Status Changes : Print Service « 2D Graphics « Java Tutorial


import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;

public class Main {
  public static void main(String[] argv) throws Exception {
    OutputStream fos = new BufferedOutputStream(new FileOutputStream(""));
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
        .lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

    StreamPrintService service = factories[0].getPrintService(fos);

    DocPrintJob job = service.createPrintJob();
    job.addPrintJobListener(new MyPrintJobListener());

class MyPrintJobListener implements PrintJobListener {
  public void printDataTransferCompleted(PrintJobEvent pje) {

  public void printJobCanceled(PrintJobEvent pje) {
    System.out.println("The print job was cancelled"); 

  public void printJobCompleted(PrintJobEvent pje) {
    System.out.println("The print job was completed"); 

  public void printJobFailed(PrintJobEvent pje) {
    System.out.println("The print job has failed");

  public void printJobNoMoreEvents(PrintJobEvent pje) {

  public void printJobRequiresAttention(PrintJobEvent pje) {

16.51.Print Service
16.51.1.List Print Services
16.51.2.Using a Printing Service
16.51.3.Using a Streaming Printing Service
16.51.4.Discovering Available Print Services
16.51.5.Discovering Available Streaming Print Services
16.51.6.Use a factory to create a print service
16.51.7.Listening for Print Service Status Changes
16.51.8.Listening for Print Job Status Changes
16.51.9.Determining the Capabilities of a Print Service
16.51.10.Determining Print Job Capabilities Supported by a Print Service