print

Get print service attributes

This is an example on how to get print service attributes in a Java Desktop Application. This is very useful when you want to list these attributes to inform the user about the properties of his print.

In short, to get print service attributes one should follow these steps:

  • Use PrintServiceLookup.lookupDefaultPrintService() to locate the default print service for this environment.
  • Use service.getAttributes().toArray() to get an array of print Attributes.

 
 
Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.Attribute;

public class GetPrintServiceAttributes {

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

		// Locate the default print service for this environment.
		PrintService service = PrintServiceLookup.lookupDefaultPrintService();

		Attribute[] attrs = service.getAttributes().toArray();

		for (int j=0; j<attrs.length; j++) {

			// Get the name of the category of which this attribute value is an instance. 
			String attrName = attrs[j].getName();
		    // get the attribute value
		    String attrValue = attrs[j].toString();

		    System.out.println("Found attribute: " + attrName + " with value: " + attrValue);

		}

	}

}

Output:

Found attribute: printer-is-accepting-jobs with value: not-accepting-jobs
Found attribute: printer-name with value: HP Universal Printing PCL 5
Found attribute: color-supported with value: not-supported
Found attribute: queued-job-count with value: 0

 
This was an example on how to get print service attributes

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He 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.

0 Comments
Inline Feedbacks
View all comments
Back to top button