栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用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;        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496123.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号