Home » Desktop Java » print » Handle print job events

About Ilias Tsagklis

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

Handle print job events

With this tutorial we shall show you how to handle print job events in a Java Desktop Application. This is a very important step to consider when you handle many print jobs in your application an you want to monitor their status as well as inform the user for the progress of his print jobs.

Basically, all you have to do in order to handle print job events in a Java application is:

  • Open a file you want to print.
  • Create DocFlavor according to the file format you want to print.
  • Create a new PrintService.
  • Create a new DocPrintJob.
  • Create a class that implements PrintJobListener.
  • Override printDataTransferCompleted, printJobCanceled, printJobCompleted, printJobFailed, printJobNoMoreEvents, printJobRequiresAttention in order to customize the handling of a wide variety of print events.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;

public class HandlePrintJobEvents {

	public static void main(String[] args) throws Exception {

		// 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.

  DocPrintJob printJob = service.createPrintJob();

  // register a listener to get notified when the job is complete

  printJob.addPrintJobListener(new PrintJobMonitor());

  // 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();

	}

	private static class PrintJobMonitor implements PrintJobListener {

		@Override
		public void printDataTransferCompleted(PrintJobEvent pje) {
			// Called to notify the client that data has been successfully
		    // transferred to the print service, and the client may free
		    // local resources allocated for that data.
		}

		@Override
		public void printJobCanceled(PrintJobEvent pje) {
			// Called to notify the client that the job was canceled
		    // by a user or a program.
		}

		@Override
		public void printJobCompleted(PrintJobEvent pje) {
			// Called to notify the client that the job completed successfully.
		}

		@Override
		public void printJobFailed(PrintJobEvent pje) {
			// Called to notify the client that the job failed to complete
		    // successfully and will have to be resubmitted.
		}

		@Override
		public void printJobNoMoreEvents(PrintJobEvent pje) {
			// Called to notify the client that no more events will be delivered.
		}

		@Override
		public void printJobRequiresAttention(PrintJobEvent pje) {
			// Called to notify the client that an error has occurred that the
		    // user might be able to fix.
		}

	}

}

 
This was an example on how to handle print job events.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of