Java中的扩展打印机信息 [英] Extended printer information in Java

查看:100
本文介绍了Java中的扩展打印机信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取有关系统上打印机的一些信息。

在Windows和Linux上,使用此代码,只有 PrinterName 属性已填充:

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));
}

使用 toArray()后上面的功能......

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

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

...结果如下:

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

如何获取更多信息,如打印机评论?

推荐答案

还有其他 PrintServiceAttribute 实现,但如果你想获取更多......

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

这只是一个代码,你可以还为doc flavor获取不受支持的值:

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.

这篇关于Java中的扩展打印机信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆