How to use Java to print on a network printer?
Solution 1
Java AWT Printing will not find a printer via a path if it is not registered to the Windows / Active Directory user that is running the printing application. You must register the printer path through Windows "Devices and Printers" as a printer for that user for it to be visible. Then, as that user, you must run lookupPrintServices
to see the list of printers available and retrieve the proper PrintService
by the exact name String
listed.
/**
* 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;
}
}
}
Solution 2
In my case there was an authentication error, I could not find shared printer because i searched them with LocalUser account, using another account or changing printer grants I could find it.
user1431544
Updated on November 06, 2020Comments
-
user1431544 over 3 years
Using Java, I need to print on network printer, which is not installed locally. I only know the printer name. All tutorials I've seen started with something like:
PrintService []services = PrinterJob.lookupPrintServices();
The problem is there can be no printers installed, so the services will be empty in that case. I need to set the printer name directly, not just enumerate through visible printers.
-
user1431544 about 12 yearsSo you are trying to tell me, that there is no way to print on unregistered printer? Thats great. Thank you.
-
JoshDM about 12 yearsWhy are you restricted from registering the network printer to the server where the application runs? It is a one-time task.
-
user1431544 about 12 yearsHonestly, I don't know. It's customers network and he has got some reason for it. I know it sucks, bu registering printers is not an option.
-
JoshDM about 12 yearsBut it's your customer's printer and your customer's server and... does your customer understand how this sort of thing actually works?
-
user1431544 about 12 yearsHe only knows that his old system can handle this:) I can print in .NET (application is written in it) with ComponentOne. Problem is, that this type of printing takes a very long time (minutes). When printer is registered, it can print in a matter of seconds (on the same physical printer). So I wanted to try it with another component and find out, that exists PDFBox, which can print pdfs, but is originaly written in Java. So now, I'm using IKVM and trying to find out how to create PrintJob, which PDFBox's print method takes as an argument.
-
JoshDM about 12 yearsPDFBox takes a
PrinterJob
according to the code. The code I posted in my answer above shows how to create aPrinterJob
. I am uncertain that without registering the printer, you can do it other ways. I suspect the reason it worked (slowly) with unregistered printer is that the printer was registering in the background, then unregistering in the background when complete, but I don't know enough to confirm that. -
user1431544 about 12 yearsMaybe, its hard to find out. We don't have this problem in our network (max 20 seconds). Yes, I need PrintJob. But its true... how can I create print job, if I don't have printer:)
-
JoshDM almost 9 years
lookupPrintServices
is not guaranteed to return any order or size, as the values available to the Windows user at any given time may vary; you will need to sort them yourself.AppContext
issun.awt.AppContext
. -
tresf almost 5 yearsUnregistered printers can (generally) be written to directly using a Socket on port 9100. This does require you to know the low-level data that the printer is expecting. Many office printers can handle PDFs natively (make sure to get the size right, PDFBOX handles sizing using the driver, which you're bypassing by using an unregistered printer).