Java中的扩展打印机信息

时间:2011-04-06 14:09:04

标签: java printing

我正在尝试获取有关系统中打印机的一些信息 在Windows和Linux上,使用此代码,仅填充PrinterName属性:

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()功能后......

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

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

2 个答案:

答案 0 :(得分:5)

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

这只是一个代码,你也可以为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()));
    }
}

注意:您可能会看到重复的值,但可以对其进行过滤。

答案 1 :(得分:3)

以下是hGx's answer中提供的模块化,更易理解的代码版本:

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

这将返回Set,其中Attributes发现给定的打印机 注意:可能会出现重复值。但是,它们可以被过滤。