This example demonstrates how to get print service’s attribute set using the javax.print
API. First we find the default printer for the current machine using the PrintServiceLookup
class. This will give us a PrintService
object, this object might be null
if no print service found.
The final step is to get the print service attribute set by calling getAttributes()
method of the PrintService
. We can convert the returned AttributeSet
into an array using the toArray()
method and iterates it.
package org.kodejava.print;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;
public class PrinterAttribute {
public static void main(String[] args) {
// Locates the default print service for this environment.
PrintService printer = PrintServiceLookup.lookupDefaultPrintService();
if (printer != null) {
// Getting print service's attribute set.
AttributeSet attributes = printer.getAttributes();
for (Attribute a : attributes.toArray()) {
String name = a.getName();
String value = attributes.get(a.getClass()).toString();
System.out.println(name + " : " + value);
}
}
}
}
An example result of this program is:
color-supported : supported
queued-job-count : 0
printer-name : HP LaserJet P1005
printer-is-accepting-jobs : accepting-jobs
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024