Example usage for javax.print CancelablePrintJob cancel

List of usage examples for javax.print CancelablePrintJob cancel

Introduction

In this page you can find the example usage for javax.print CancelablePrintJob cancel.

Prototype

public void cancel() throws PrintException;

Source Link

Document

Stops further processing of a print job.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    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 {/*from  w  ww  .j av  a2  s.c o m*/
            cancelJob.cancel();
        } catch (PrintException e) {
        }
    }
    job.print(doc, null);
    pjDone.waitForDone();
    is.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    try {/*from  w w w . jav 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");
        }
    }
}