This discussion is archived
8 Replies Latest reply: Oct 30, 2012 4:14 AM by nickarls RSS

How to get a PrinterState

944511 Newbie
Currently Being Moderated
Hi,

I'm not to be able to get the PrinterState of my Printers. I have tested the following code on Linux and on Windows (7) with different Printers (Samsung, Brother (only on Linux), Zebra, ...). But in every case the PrinterState and the PrinterStateReasons objects are null.
private void dumpPrinterState() {
  PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); //get printers

  for (PrintService printService : printServices) {
    log.info("---- printer: " + printService);
    log.info("---- state: " + printService.getAttribute(PrinterState.class));
    log.info("---- reason: " + printService.getAttribute(PrinterStateReasons.class));
  }
}
Here is the output with my try on Windows:
---- printer: Win32 Printer : ZDesigner KR403
---- state: null
---- reason: null
---- printer: Win32 Printer : Samsung CLP-300 Series
---- state: null
---- reason: null
---- printer: Win32 Printer : Microsoft XPS Document Writer
---- state: null
---- reason: null
---- printer: Win32 Printer : Fax
---- state: null
---- reason: null
Anybody knows whats wrong?

Thanks,
Steffen
  • 1. Re: How to get a PrinterState
    mlvenkatesh Newbie
    Currently Being Moderated
    Do you want to know the printer job status? What do you mean by printer state ?
  • 2. Re: How to get a PrinterState
    944511 Newbie
    Currently Being Moderated
    Hi,

    thanks for your reply.

    No. I mean the state of the printer: if the printer is waiting for new jobs (javax.print.attribute.standard.PrintState.IDLE), is the printer is printing some stuff (javax.print.attribute.standard.PrintState.PROCESSING), is the printer is stopped (javax.print.attribute.standard.PrintState.STOPPED). If the printer is stopped I need the reason for that (javax.print.attribute.standard.PrinterStateReason(s)): out of paper, door open, ...

    But in every case if I want to access the PrinterState or the PrinterStateReasons object the objects are null (on Linux and Windows and for all printers).

    Steffen
  • 3. Re: How to get a PrinterState
    mlvenkatesh Newbie
    Currently Being Moderated
    Hi

    You have to specify the attributes your print service must support while loking up using PrintServiceLookup.lookupPrintServices();

    In your case its null. Please provide the attribute(PrinterState) while looking up.


    Let me know if this works
  • 4. Re: How to get a PrinterState
    944511 Newbie
    Currently Being Moderated
    I change the code now to:
        AttributeSet attrs = new HashPrintServiceAttributeSet(PrinterState.STOPPED);
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, attrs); //get printers
       
        for (PrintService printService : printServices) {
          log.info("---- printer: " + printService);
          log.info("---- state: " + printService.getAttribute(PrinterState.class));
          log.info("---- reason: " + printService.getAttribute(PrinterStateReasons.class));
        }
    and I was hoping that I was getting now only printers which has a failed (out of paper or so). But on Windows I'm getting all printers (and for each printer is the PrinterState and PrinterStateReasons still null). On Linux I getting no printers. Same result if I use PrinterState.IDLE or a other state.
  • 5. Re: How to get a PrinterState
    mlvenkatesh Newbie
    Currently Being Moderated
    Hi,

    Please check if the printer is supporting the attributes PrinterState and PrinterStateReasons using PrinterService.getSupportedActivities().

    only if the these attributes are supported you can get what you want.
  • 6. Re: How to get a PrinterState
    mlvenkatesh Newbie
    Currently Being Moderated
    i tried this on my machine and never got PrinterState attribute in the list of supported categories
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null,null);
              
              for(PrintService printService : printServices){
                   Set<Attribute> attribSet = new LinkedHashSet<Attribute>(); 
                   Class<? extends Attribute>[] attributeCategory = (Class<? extends Attribute>[])printService.getSupportedAttributeCategories();
                   for(Class<? extends Attribute> category:attributeCategory){
                        for(DocFlavor flavor: printService.getSupportedDocFlavors()){
                             Object value = printService.getSupportedAttributeValues(category, flavor, printService.getAttributes());             
                             if (value != null) {   
                                                                     
                                  if (value instanceof Attribute){
                                       
                                       attribSet.add((Attribute) value);
                                  }                 
                                  else if (value instanceof Attribute[]) {
                                       
                                       attribSet.addAll(Arrays.asList((Attribute[]) value));
                                  }
                             }
                             
                        }
                   }
                   for (Attribute attr : attribSet) {  
                        System.out.println(attr.getName());  
                          
                   } 
              }
              
         }
         
  • 7. Re: How to get a PrinterState
    944511 Newbie
    Currently Being Moderated
    Hi,

    thank you very much for your efforts.

    I see no way to get the PrinterState/PrinterStateReason object yet.

    At this time I'm working as a workaround on a JNI library to get the printer state (on windows at first).

    Thanks,
    Steffen
  • 8. Re: How to get a PrinterState
    nickarls Newbie
    Currently Being Moderated
    I'm also interested in this. I find it strange that none of my printers appear to support checking e.g. that it's out of paper (Win 7/64 on JDK 7)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points