print

Discover Print Services

With this example we shall show you how to discover print services in a Java Desktop Application. You will find this particularly useful when you want to create and handle print jobs from you application.

Discovering streaming print services is very easy as it requires that you :

  • Use PrintServiceLookup.lookupPrintServices to locates factories for print services that can be used with a print.
  • Use PrintServiceLookup.lookupDefaultPrintService() to look up the default print service.
  • Use PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.JPEG, null); to find services that support a particular input format (e.g. JPEG).
  • Then find printer service by name and find find services that support a set of print job capabilities (e.g. color) using PrintServiceLookup.lookupPrintServices

Let’s see the code snippet that follows:

package com.javacodegeeks.snippets.desktop;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.ColorSupported;
import javax.print.attribute.standard.PrinterName;

public class DiscoverPrintServices {

	public static void main(String[] args) {

		// locate print services capable of printing the specified DocFlavor and attributes
		// with null no constraints are used
		PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

		System.out.println("Printer Services found:");
		printService(services);

		// Look up the default print service
		PrintService service = PrintServiceLookup.lookupDefaultPrintService();

		if (service!=null) {
			System.out.println("Default Printer Service found:");
			System.out.println("t" + service);
		}

		// find services that support a particular input format (e.g. JPEG)
		services = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.JPEG, null);
		System.out.println("Printer Services with JPEG support:");
		printService(services);

		// find printer service by name
		AttributeSet aset = new HashAttributeSet();
		aset.add(new PrinterName("Microsoft XPS Document Writer", null));
		services = PrintServiceLookup.lookupPrintServices(null, aset);

		System.out.println("Printer Service Microsoft XPS Document Writer:");
		printService(services);

		// find services that support a set of print job capabilities (e.g. color)
		aset = new HashAttributeSet();
		aset.add(ColorSupported.SUPPORTED);
		services = PrintServiceLookup.lookupPrintServices(null, aset);

		System.out.println("Printer Services with color support:");
		printService(services);

	}

	private static void printService(PrintService[] services) {
		if (services!=null && services.length>0) {
			for (int i = 0; i < services.length; i++) {
				System.out.println("t" + services[i]);
			}
		}
	}

}

Output:

Printer Services found:
	Win32 Printer : Microsoft XPS Document Writer
	Win32 Printer : HP Universal Printing PCL 5
	Win32 Printer : Fax
	Win32 Printer : \http://10.42.76.11:631Photosmart_C3100_series
Default Printer Service found:
	Win32 Printer : HP Universal Printing PCL 5
Printer Services with JPEG support:
	Win32 Printer : Microsoft XPS Document Writer
	Win32 Printer : HP Universal Printing PCL 5
	Win32 Printer : Fax
Printer Service Microsoft XPS Document Writer:
	Win32 Printer : Microsoft XPS Document Writer
Printer Services with color support:
	Win32 Printer : Microsoft XPS Document Writer
	Win32 Printer : HP Universal Printing PCL 5
	Win32 Printer : Fax

 
This was an example on how to discover Print Services.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Naveen
4 years ago

Very Good information Thank you so much…

Back to top button