Cancelling a Print Job : Print Job « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » Print JobScreenshots 
Cancelling a Print Job
  

import java.awt.print.PrinterAbortException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.print.CancelablePrintJob;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.SimpleDoc;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class Main {
  public static void main(String[] argvthrows Exception {
    try {
      OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
      DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
      InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));
      StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
          .lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

      StreamPrintService service = factories[0].getPrintService(fos);
      final DocPrintJob job = service.createPrintJob();
      Doc doc = new SimpleDoc(is, flavor, null);

      PrintJobWatcher pjDone = new PrintJobWatcher(job);

      if (job instanceof CancelablePrintJob) {
        CancelablePrintJob cancelJob = (CancelablePrintJobjob;
        try {
          cancelJob.cancel();
        catch (PrintException e) {
        }
      }
      job.print(doc, null);
      pjDone.waitForDone();
      is.close();
    catch (PrintException e) {
      if (e.getCause() instanceof PrinterAbortException) {
        System.out.println("Print job was cancelled")
      }
    }
  }
}

class PrintJobWatcher {
  boolean done = false;

  PrintJobWatcher(DocPrintJob job) {
    job.addPrintJobListener(new PrintJobAdapter() {
      public void printJobCanceled(PrintJobEvent pje) {
        synchronized (PrintJobWatcher.this) {
          done = true;
          PrintJobWatcher.this.notify();
        }
      }

      public void printJobCompleted(PrintJobEvent pje) {
        synchronized (PrintJobWatcher.this) {
          done = true;
          PrintJobWatcher.this.notify();
        }
      }

      public void printJobFailed(PrintJobEvent pje) {
        synchronized (PrintJobWatcher.this) {
          done = true;
          PrintJobWatcher.this.notify();
        }
      }

      public void printJobNoMoreEvents(PrintJobEvent pje) {
        synchronized (PrintJobWatcher.this) {
          done = true;
          PrintJobWatcher.this.notify();
        }
      }
    });
  }

  public synchronized void waitForDone() {
    try {
      while (!done) {
        wait();
      }
    catch (InterruptedException e) {
    }
  }
}

   
    
  
Related examples in the same category
1.Determining Print Job Capabilities Supported by a Print Service
2.Getting the Default Value of a Print Job Capability
3.Getting the Possible Values for a Print Job Capability
4.Setting the Orientation of a Print Job -- Portrait or Landscape
5.Setting the Number of Copies of a Print Job
6.Listening for Print Job Status Changes
7.Listening for Print Job Attribute Changes
8.Determining When a Print Job Has Finished
9.How to print 2D graphicsHow to print 2D graphics
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.