how to use java code to print with a network printer code example
Example: how to use java code to print with a network printer
public static PrintService findPrintService(String printerName) {
PrintService service = null;
PrintService[] services = PrinterJob.lookupPrintServices();
for (int index = 0; service == null && index < services.length; index++) {
if (services[index].getName().equalsIgnoreCase(printerName)) {
service = services[index];
}
}
return service;
}
public static PrinterJob findPrinterJob(String printerName) throws Exception {
PrintService printService = PrintUtility.findPrintService(printerName);
if (printService == null) {
throw new IllegalStateException("Unrecognized Printer Service \"" + printerName + '"');
}
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintService(printService);
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;
}
}
}