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 DocFlavor using DocFlavor.INPUT_STREAM.PDF that will create.
  • Locate the default print service for this environment using PrintServiceLookup.lookupDefaultPrintService()
  • Create and return a DocPrintJob capable of handling data from any of the supported document flavors using service.createPrintJob().
  • Start the print job with printJob.print.
  • Use (CancelablePrintJob) printJob to create a
  • CancelablePrintJob and 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 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);


		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.
		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);

		// set flow layout for the frame
		frame.getContentPane().setLayout(new FlowLayout());

		JButton printButton = new JButton("Print");

		printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

		JButton cancelButton = new JButton("Cancel");

		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {



	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() {




This was an example on how to cancel print job.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

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

Inline Feedbacks
View all comments
Back to top button