I’m trying to find the printer tray ids of a particular printer. I have searched and found a lot of articles in Java using the PrintService
, Media and MediaTray
. Yet, this doesn’t provide a solution for my requirement. I will get the following output if I run the below code.
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 Printer : 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
If I run the following .net code I would get the following outpt.
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);
}
Automatically Select 15
MP Tray 261
Lower Cassette 1 260
Lower Cassette 2 259
Lower Cassette 3 258
Lower Cassette 4 257
The above is what I want to achieve. I want to get the raw printer ID like 260, 259, 258 and so on through Java instead of getting 7,8,9 and 10. Is this possible in some way?
To your above code just add 3 more lines. All you need is to print winID property from "Win32MediaTray" object.