打印机媒体托盘的名称

3
我目前使用以下代码片段检索有关可能介质托盘的打印机信息:
 Media med[] = (Media[])printService.getSupportedAttributeValues(Media.class, null, null);
 if( med != null ) {    
     for (int k=0; k<med.length; k++) {
          sb.append("Name : " + med[k].getClass() + " - Value : " + med[k].getValue() +
                " - Name : " + med[k].getName()+"\n" );
     }
 }

很遗憾,这只是返回一些托盘号码,我觉得相对没什么用处。
Name : class javax.print.attribute.standard.MediaSizeName - Value : 40 - Name : media
Name : class javax.print.attribute.standard.MediaSizeName - Value : 41 - Name : media
Name : class javax.print.attribute.standard.MediaSizeName - Value : 42 - Name : media
... more ...
Name : class sun.print.Win32MediaTray - Value : 5 - Name : media
Name : class sun.print.Win32MediaTray - Value : 25 - Name : media
Name : class sun.print.Win32MediaTray - Value : 26 - Name : media
Name : class sun.print.Win32MediaTray - Value : 27 - Name : media

如何获取有用的名称?
2个回答

5

这将为您提供所有打印机属性,包括托盘名称。

String printName = "HP Officejet Pro 8500 A910 (Network)";
AttributeSet aset = new HashAttributeSet();
aset.add(new PrinterName(printName, null));
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, aset);
for (int i = 0; i < services.length; i++) {
  PrintService service = services[i];
  System.out.println(service);
  DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
  Object o = service.getSupportedAttributeValues(Media.class, flavor, null);
  if (o != null && o.getClass().isArray()) {
    for (Media media : (Media[]) o) {
      System.out.println(media + " ID: " + media.getValue() + "\t" + media.getClass().getName());
    }
  }
}

media.toString()(这是你在println中所做的)提供了一个“有用”的名称;必须区分MediaTray类,它表示实际的托盘,和MediaSizeName类,它表示媒体尺寸。这些不能作为getSupportedAttributeValues()的类参数使用,仍然需要使用Media.class。真是太麻烦了。 - Tilman Hausherr

-1

这段代码通过尺寸和ID获取打印机名称和介质盘名称:

public static void printByName(String printName) {
//Inicio Codigo PrintByName
    try{
        AttributeSet aset = new HashAttributeSet();
        aset.add(new PrinterName(printName, null));
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null,aset);
    MediaSizeName mn=null;
    MediaSize mz=null;
    Destination dd=null;
    for (int i = 0; i < services.length; i++) {
        PrintService service = services[i];
        System.out.println(service);
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
        Class category = Media.class;
        Object o = service.getSupportedAttributeValues(category, flavor,null);
        if (o == null) { // Attribute is not supported
            } 
        else if (o.getClass().isArray()) {
            // Attribute values are a set of values
            Media[] media = (Media[]) o;
            for (int j = 0; j < media.length; j++) {
                //System.out.println("ATributo: " + media[j].getValue() );
                if(media[j] instanceof MediaSizeName) {
                mn=(MediaSizeName)media[j].clone();
                //mz=getMediaSizeForName(mn);
                mz=MediaSize.getMediaSizeForName(mn);

                //System.out.println((media[j].toString()));
                System.out.println("\t Bandeja: \t\t"+ mn + " ID: " + mn.getValue() );
                }
                }
            }
        }
    //System.exit(0);
    } catch(Exception e){
    System.out.println("Exception is " + e);
            }    
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接