Under the Users' requirements, when users click a "Print" button, the PDF/Word Doc will be directly transferred to the default Printer and then print the reports out.
Actually, it is required to pass the username and password to the default printer. It is because printing privilege is set. If username is not found or password is not correct, it is not allowed to print anything. However, I don't know how the pass the correct username and password to the printer via Java
Here is my code:
public void testGhostScriptTest() throws MalformedURLException, PrintException{ //set the job attributes PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); attributes.add(new RequestingUserName("IT", null)); //Set the username as IT PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService(); if(defaultPrintService==null){ //TODO: throw exception } DocPrintJob printerJob = defaultPrintService.createPrintJob(); File pdfFile = new File("C:\\temp\\1372733129110.pdf"); SimpleDoc simpleDoc = new SimpleDoc(pdfFile.toURL(), DocFlavor.URL.AUTOSENSE, null); printerJob.print(simpleDoc, attributes); }
After running the UnitTest, there is a message shown at the tray bar. The message informs me that the report is sent to the printer and printed. However, I found an error on the printer console that login is invalid. On the printer console, "PRINT" is displayed as username.
Could anyone tell me how to set/pass the correct username and password to the print via java?
Thanks for your kind attention~