Home » Desktop Java » print » Get print service attributes

About Ilias Tsagklis

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

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

(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