6

I need to have more control about my printer and then I'm trying to get PrinterState of my printer and then use PrintStareReasons. My code is the following:

public void checkPrinterStatus(){

    try {
        logger.info("Check -------------- ");

        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    PrintService printer = configParamPrintService.getPrintService();
    logger.info("State " + printer.isAttributeCategorySupported(PrinterState.class));
    Set<Attribute> attributes = getAttributes(printer);
    for(Attribute attr : attributes){
        logger.info(attr.getName());
    }



}

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's something
            if (value != null) {
                //if it's a SINGLE attribute...
                if (value instanceof Attribute)
                    set.add((Attribute) value); //...then add it

                //if it's a SET of attributes...
                else if (value instanceof Attribute[])
                    set.addAll(Arrays.asList((Attribute[]) value)); //...then add its childs
            }
        }
    }

    return set;
}

Googling I also write getAttributes() to get all attribures but PrinterState is not present.

This is the list of all attributes:

21.12.2015 16:48:56 INFO  PrintWorker:142 - Check --------------
21.12.2015 16:48:58 INFO  PrintWorker:151 - State false
21.12.2015 16:48:58 INFO  PrintWorker:154 - copies-supported
21.12.2015 16:48:58 INFO  PrintWorker:154 - finishings
21.12.2015 16:48:58 INFO  PrintWorker:154 - job-sheets
21.12.2015 16:48:58 INFO  PrintWorker:154 - job-sheets
21.12.2015 16:48:58 INFO  PrintWorker:154 - number-up
21.12.2015 16:48:58 INFO  PrintWorker:154 - number-up
21.12.2015 16:48:58 INFO  PrintWorker:154 - number-up
21.12.2015 16:48:58 INFO  PrintWorker:154 - number-up
21.12.2015 16:48:58 INFO  PrintWorker:154 - number-up
21.12.2015 16:48:58 INFO  PrintWorker:154 - number-up
21.12.2015 16:48:58 INFO  PrintWorker:154 - orientation-requested
21.12.2015 16:48:58 INFO  PrintWorker:154 - orientation-requested
21.12.2015 16:48:58 INFO  PrintWorker:154 - orientation-requested
21.12.2015 16:48:58 INFO  PrintWorker:154 - orientation-requested
21.12.2015 16:48:58 INFO  PrintWorker:154 - page-ranges
21.12.2015 16:48:58 INFO  PrintWorker:154 - media
21.12.2015 16:48:58 INFO  PrintWorker:154 - spool-data-destination

While

logger.info("State " + printer.isAttributeCategorySupported(PrinterState.class));

return always:

21.12.2015 16:48:58 INFO  PrintWorker:151 - State false

I have tested the following code on Linux and on Windows (7), but none of them return the actual status. What could be the problem? The printer, the Driver or my code?

2

1 Answer 1

6
+25

isAttributeCategorySupported() returns true if the print service supports specifying a doc-level or job-level attribute in category in a Print Request otherwise returns false.

having a look at the official oracle documentation will make my point much clearer

1
  • Then you told me that my print service doesn't support this kind of attribute? And then I'll never get these attributes?
    – Skizzo
    Dec 31, 2015 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.