如何获取原始打印机托盘ID

时间:2013-08-09 10:52:12

标签: java printing tray

我正在尝试查找特定打印机的打印机托盘ID。我使用PrintService,媒体和MediaTray在Java中搜索并发现了很多文章。然而,这并没有为我的要求提供解决方案。如果我运行以下代码,我将得到以下输出。

String printName = "AL-M2010-3D8E02";
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) {
            if (media instanceof MediaTray) {
                System.out.println(media + " ID: " + media.getValue() + "\t" + media.getClass().getName());
            }
        }
    }
}

Win32打印机:AL-M2010-3D8E02

Form-Source ID:5 sun.print.Win32MediaTray

MP Tray ID:6 sun.print.Win32MediaTray

Lower Cassette 1 ID:7 sun.print.Win32MediaTray

Lower Cassette 2 ID:8 sun.print.Win32MediaTray

Lower Cassette 3 ID:9 sun.print.Win32MediaTray

Lower Cassette 4 ID:10 sun.print.Win32MediaTray

如果我运行以下.net代码,我会得到以下内容。

PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = "AL-M2010-3D8E02";

for (int i = 0; i < printDoc.PrinterSettings.PaperSources.Count; i++)
{
    trayDetails.AppendText(printDoc.PrinterSettings.PaperSources[i].SourceName + " " + printDoc.PrinterSettings.PaperSources[i].RawKind.ToString());
    trayDetails.AppendText(Environment.NewLine);
}

自动选择15

MP Tray 261

降低纸盒1 260

降低纸盒2 259

降低纸盒3 258

降低纸盒4 257

以上是我想要实现的目标。我希望通过Java获得原始打印机ID,如260,259,258等,而不是获得7,8,9和10.这在某种程度上是否可行?

1 个答案:

答案 0 :(得分:1)

对于上面的代码,只需再添加3行。您只需要打印来自&#34; Win32MediaTray&#34;的winID属性。对象

if (o != null && o.getClass().isArray()) {
        for (Media media : (Media[]) o) {
            if (media instanceof MediaTray) {
                System.out.println(media + " ID: " + media.getValue() + "\t" + media.getClass().getName());

                 if(media instanceof sun.print.Win32MediaTray){
                      Win32MediaTray  win32Tray = (Win32MediaTray)media;
                      System.out.println(win32Tray.winID);
                  }

            }
        }
    }