如何使用Java在网络打印机上进行打印?

如何使用Java在网络打印机上进行打印?,第1张

如何使用Java在网络打印上进行打印

如果Java AWT Printing未向运行打印应用程序的Windows / Active
Directory用户注册,则无法通过路径找到打印机。您必须通过Windows“设备和打印机”将打印机路径注册为该用户的打印机,才能使其可见。然后,您必须运行

lookupPrintServices
以查看可用的打印机列表,并
PrintService
通过
String
列出的确切名称检索正确的打印机。

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


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5488244.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存