Cancel

try {
    // Create the print job
    final DocPrintJob job = service.createPrintJob();
    Doc doc = new SimpleDoc(is, flavor, null);

    // Monitor print job events.
    // See Determining When a Print Job Has Finished
    // for the implementation of PrintJobWatcher.
    PrintJobWatcher pjDone = new PrintJobWatcher(job);

    // Sample code to display a cancel button
    JFrame frame = new JFrame();
    if (job instanceof ablePrintJob) {
        JButton btn = new JButton(" Print Job");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                ablePrintJob cancelJob = (ablePrintJob)job;
                try {
                    cancelJob.cancel();
                } catch (PrintException e) {
                    // Possible reason is job was already finished
                }
            }
        });

        frame.getContentPane().add(btn, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    // Print it
    job.print(doc, null);

    // Wait for the print job to be done
    pjDone.waitForDone();

    // Remove frame
    frame.setVisible(false);

    // It is now safe to close the input stream
    is.close();
} catch (PrintException e) {
    if (e.getCause() instanceof java.awt.print.PrinterAbortException) {
        // Print job was cancelled
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.