admin管理员组文章数量:1618197
小编典典
如果Java AWT Printing未向运行打印应用程序的Windows / Active
Directory用户注册,则无法通过路径找到打印机。您必须通过Windows“设备和打印机”将打印机路径注册为该用户的打印机,才能使其可见。然后,您必须运行lookupPrintServices以查看可用的打印机列表,并PrintService通过String列出的确切名称检索正确的打印机。
/**
* Retrieve the specified Print Service; will return null if not found.
* @return
*/
public static PrintService findPrintService(String printerName) {
PrintService service = null;
// Get array of all print services - sort order NOT GUARANTEED!
PrintService[] services = PrinterJob.lookupPrintServices();
// Retrieve specified print service from the array
for (int index = 0; service == null && index < services.length; index++) {
if (services[index].getName().equalsIgnoreCase(printerName)) {
service = services[index];
}
}
// Return the print service
return service;
}
/**
* Retrieve a PrinterJob instance set with the PrinterService using the printerName.
*
* @return
* @throws Exception IllegalStateException if expected printer is not found.
*/
public static PrinterJob findPrinterJob(String printerName) throws Exception {
// Retrieve the Printer Service
PrintService printService = PrintUtility.findPrintService(printerName);
// Validate the Printer Service
if (printService == null) {
throw new IllegalStateException("Unrecognized Printer Service \"" + printerName + '"');
}
// Obtain a Printer Job instance.
PrinterJob printerJob = PrinterJob.getPrinterJob();
// Set the Print Service.
printerJob.setPrintService(printService);
// Return Print Job
return printerJob;
}
/**
* Printer list does not necessarily refresh if you change the list of
* printers within the O/S; you can run this to refresh if necessary.
*/
public static void refreshSystemPrinterList() {
Class[] classes = PrintServiceLookup.class.getDeclaredClasses();
for (int i = 0; i < classes.length; i++) {
if ("javax.print.PrintServiceLookup$Services".equals(classes[i].getName())) {
sun.awt.AppContext.getAppContext().remove(classes[i]);
break;
}
}
}
2020-09-24
版权声明:本文标题:java网络打印机_如何使用Java在网络打印机上进行打印? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1728778022a1172683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论