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 :

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.

Exit mobile version