print
Cancel print job
This is an example that demonstrates how to cancel print jobs in a Java Desktop Application. This is a very basic step you have to consider when you have to develop error handling procedures for your applications.
In short, all you have to do to cancel print jobs is:
- Open a pdf file you want to print.
- Create a PDF
DocFlavorusingDocFlavor.INPUT_STREAM.PDFthat will create. - Locate the default print service for this environment using
PrintServiceLookup.lookupDefaultPrintService() - Create and return a
DocPrintJobcapable of handling data from any of the supported document flavors usingservice.createPrintJob(). - Start the print job with
printJob.print. - Use
(CancelablePrintJob) printJobto create a CancelablePrintJoband use cancelableJob.cancel() to cancel the print job.
Let’s see the code snippet that follows:
package com.javacodegeeks.snippets.desktop;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.print.CancelablePrintJob;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.swing.JButton;
import javax.swing.JFrame;
public class CancelPrintJob {
private static DocPrintJob printJob;
private static void printDocument() {
try {
// Open the image file
InputStream is = new BufferedInputStream(new FileInputStream("myfile.pdf"));
// create a PDF doc flavor
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
// Locate the default print service for this environment.
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
// Create and return a PrintJob capable of handling data from
// any of the supported document flavors.
printJob = service.createPrintJob();
// Construct a SimpleDoc with the specified
// print data, doc flavor and doc attribute set.
Doc doc = new SimpleDoc(is, flavor, null);
// Print a document with the specified job attributes.
printJob.print(doc, null);
is.close();
}
catch (PrintException e) {
System.out.println("Failed to cancel printing:" + e.getMessage());
}
catch (IOException e) {
System.out.println("I/O Error:" + e.getMessage());
}
}
private static void cancelPrinting() {
try {
CancelablePrintJob cancelableJob = (CancelablePrintJob) printJob;
// Stops further processing of a print job.
cancelableJob.cancel();
}
catch (PrintException e) {
System.out.println("Failed to cancel printing:" + e.getMessage());
}
}
private static void createAndShowGUI() {
// Create and set up the window.
final JFrame frame = new JFrame("Cancel Print Example");
// Display the window.
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set flow layout for the frame
frame.getContentPane().setLayout(new FlowLayout());
JButton printButton = new JButton("Print");
printButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
printDocument();
}
});
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cancelPrinting();
}
});
frame.getContentPane().add(printButton);
frame.getContentPane().add(cancelButton);
}
public static void main(String[] args) throws Exception {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
This was an example on how to cancel print job.
