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 {
                } catch (PrintException e) {
                    // Possible reason is job was already finished

        frame.getContentPane().add(btn, BorderLayout.CENTER);

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

    // Wait for the print job to be done

    // Remove frame

    // It is now safe to close the input stream
} 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.