Example usage for javax.print PrintException getCause

List of usage examples for javax.print PrintException getCause

Introduction

In this page you can find the example usage for javax.print PrintException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    try {/*from ww  w  . j a  v a 2  s.c  o m*/
        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 = (CancelablePrintJob) job;
            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");
        }
    }
}