
如果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; } }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)