Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

printing - Extended printer information in Java

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:

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 do I get more information, like the printer comment?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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<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 may see repeated values, but they can be filtered.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...