Extended printer information in Java

后端 未结 2 1547
礼貌的吻别
礼貌的吻别 2020-12-11 04:09

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

2条回答
  •  臣服心动
    2020-12-11 04:51

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

    This is a dirty code only, you can also fetch unsupported values for doc flavor:

    PrintService[] printServices =
            PrintServiceLookup.lookupPrintServices(null, null); //get printers
    
    for (PrintService printService : printServices) {
        System.out.println("Found print service: " + printService);
    
        Set attribSet = new LinkedHashSet();
    
        Class[] supportedAttributeCategories = (Class[]) printService.getSupportedAttributeCategories();
    
        for (Class 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 may see repeated values, but they can be filtered.

提交回复
热议问题