Advanced Printer Information in Java

I am trying to get some information about printers on my system.
On Windows and Linux, only the PrinterName attribute is PrinterName with this code:

 PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null,null); for( PrintService printService : printServices ) { log.info("Found print service: "+printService); log.info(printService.getAttribute(PrinterName.class)); log.info(printService.getAttribute(PrinterLocation.class)); log.info(printService.getAttribute(PrinterMakeAndModel.class)); log.info(printService.getAttribute(PrinterMessageFromOperator.class)); log.info(printService.getAttribute(PrinterMoreInfo.class)); log.info(printService.getAttribute(PrinterMoreInfoManufacturer.class)); log.info(printService.getAttribute(PrinterState.class)); log.info(printService.getAttribute(PrinterStateReasons.class)); log.info(printService.getAttribute(PrinterURI.class)); } 

After using the toArray() function on it ...

 log.info("Found print service: "+printService); for( Attribute a : printService.getAttributes().toArray() ) { log.info("* "+a.getName()+": "+a); } 

... this is the result:

  Found print service: Win32 Printer: Brother MFC-9420CN BR-Script3
 * color-supported: supported
 * printer-name: Brother MFC-9420CN BR-Script3
 * printer-is-accepting-jobs: accepting-jobs
 * queued-job-count: 0 

How to get additional information , for example, a comment on a printer?

+7
source share
2 answers

There are other implementations of PrintServiceAttribute , but if you want more ...

This is just dirty code, you can also get unsupported values ​​for the doc flavor:

 PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); //get printers for (PrintService printService : printServices) { System.out.println("Found print service: " + printService); Set<Attribute> attribSet = new LinkedHashSet<Attribute>(); Class<? extends Attribute>[] supportedAttributeCategories = (Class<? extends Attribute>[]) printService.getSupportedAttributeCategories(); for (Class<? extends Attribute> category : supportedAttributeCategories) { DocFlavor[] flavors = printService.getSupportedDocFlavors(); for (DocFlavor flavor : flavors) { Object supportedAttributeValues = printService.getSupportedAttributeValues(category, flavor, printService.getAttributes()); if (supportedAttributeValues instanceof Attribute) { Attribute attr = (Attribute) supportedAttributeValues; attribSet.add(attr); } else if (supportedAttributeValues != null) { Attribute[] attrs = (Attribute[]) supportedAttributeValues; for (Attribute attr : attrs) { attribSet.add(attr); } } } } for (Attribute attr : attribSet) { System.out.println(attr.getName()); System.out.println(printService.getDefaultAttributeValue(attr.getCategory())); } } 

Note. You can see duplicate values, but they can be filtered.

+3
source

Below is a modular, more understandable version of the code provided in hGx answer :

 public static Set<Attribute> getAttributes(PrintService printer) { Set<Attribute> set = new LinkedHashSet<Attribute>(); //get the supported docflavors, categories and attributes Class<? extends Attribute>[] categories = (Class<? extends Attribute>[]) printer.getSupportedAttributeCategories(); DocFlavor[] flavors = printer.getSupportedDocFlavors(); AttributeSet attributes = printer.getAttributes(); //get all the avaliable attributes for (Class<? extends Attribute> category : categories) { for (DocFlavor flavor : flavors) { //get the value Object value = printer.getSupportedAttributeValues(category, flavor, attributes); //check if it something if (value != null) { //if it a SINGLE attribute... if (value instanceof Attribute) set.add((Attribute) value); //...then add it //if it a SET of attributes... else if (value instanceof Attribute[]) set.addAll(Arrays.asList((Attribute[]) value)); //...then add its childs } } } return set; } 

This will return Set with the Attributes found for this printer.
Note. Duplicate values ​​may appear. However, they can be filtered.

+3
source

All Articles