static void thirdPartyPrintLogic(String printerName) throws Exception { PrinterJob printerjob = PrinterJob.getPrinterJob(); printerjob.setCopies(2); printerjob.setJobName("myJobName"); printerjob.setPrintable(new DummyPrintable()); for (PrintService printService : PrinterJob.lookupPrintServices()) { System.out.println("check printer name of service " + printService); if (printerName.equals(printService.getName())) { System.out.println("correct printer service do print..."); printerjob.setPrintService(printService); printerjob.print(); break; } } }
private int addPrintServiceToList(ArrayList<PrintService> printerList, PrintService ps) { int index = printerList.indexOf(ps); // Check if PrintService with same name is already in the list. if (CUPSPrinter.isCupsRunning() && index != -1) { // Bug in Linux: Duplicate entry of a remote printer // and treats it as local printer but it is returning wrong // information when queried using IPP. Workaround is to remove it. // Even CUPS ignores these entries as shown in lpstat or using // their web configuration. PrinterURI uri = ps.getAttribute(PrinterURI.class); if (uri.getURI().getHost().equals("localhost")) { IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, ignoring the new local printer: "+ps); return index; // Do not add this. } PrintService oldPS = printerList.get(index); uri = oldPS.getAttribute(PrinterURI.class); if (uri.getURI().getHost().equals("localhost")) { IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, removing existing local printer: "+oldPS); printerList.remove(oldPS); } else { return index; } } printerList.add(ps); return (printerList.size() - 1); }
/** * Starts the application. */ public static void printTest() { System.out.println("\nDefault print service: " + PrintServiceLookup.lookupDefaultPrintService()); System.out.println("is flavor: "+flavor+" supported? "+ services[0].isDocFlavorSupported(flavor)); System.out.println("is Page Ranges category supported? "+ services[0].isAttributeCategorySupported(PageRanges.class)); System.out.println("is PageRanges[2] value supported ? "+ services[0].isAttributeValueSupported( new PageRanges(2), flavor, null)); HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet(); //prSet.add(new PageRanges(2)); PrintService selService = ServiceUI.printDialog(null, 200, 200, services, services[0], flavor, prSet); System.out.println("\nSelected Values\n"); Attribute attr[] = prSet.toArray(); for (int x = 0; x < attr.length; x ++) { System.out.println("Attribute: " + attr[x].getName() + " Value: " + attr[x]); } }
protected static PrintService lookupDefaultPrintService() { PrintService service = PrintServiceLookup.lookupDefaultPrintService(); /* Pageable implies Printable so checking both isn't strictly needed */ if (service != null && service.isDocFlavorSupported( DocFlavor.SERVICE_FORMATTED.PAGEABLE) && service.isDocFlavorSupported( DocFlavor.SERVICE_FORMATTED.PRINTABLE)) { return service; } else { PrintService []services = PrintServiceLookup.lookupPrintServices( DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); if (services.length > 0) { return services[0]; } } return null; }
private void setPrinterNameAttrib(String printerName) { PrintService service = this.getPrintService(); if (printerName == null) { return; } if (service != null && printerName.equals(service.getName())) { return; } else { PrintService []services = PrinterJob.lookupPrintServices(); for (int i=0; i<services.length; i++) { if (printerName.equals(services[i].getName())) { try { this.setPrintService(services[i]); } catch (PrinterException e) { } return; } } } //** END Functions called by native code for querying/updating attributes }
@SuppressWarnings("unchecked") // Cast to Class<PrintServiceAttribute> boolean matchingService(PrintService service, PrintServiceAttributeSet serviceSet) { if (serviceSet != null) { Attribute [] attrs = serviceSet.toArray(); Attribute serviceAttr; for (int i=0; i<attrs.length; i++) { serviceAttr = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory()); if (serviceAttr == null || !serviceAttr.equals(attrs[i])) { return false; } } } return true; }
protected void updatePageAttributes(PrintService service, PageFormat page) { if (this.attributes == null) { this.attributes = new HashPrintRequestAttributeSet(); } updateAttributesWithPageFormat(service, page, this.attributes); }
public static void main(String[] args) throws Exception { for (PrintService service : PrintServiceLookup.lookupPrintServices(null, null)) { String serviceName = service.getName(); PrinterName name = service.getAttribute(PrinterName.class); String printerName = name.getValue(); PrintService serviceByName = lookupByName(printerName); System.out.println("service " + service); System.out.println("serviceByName " + serviceByName); if (!service.equals(serviceByName)) { throw new RuntimeException("NOK " + serviceName + " expected: " + service.getClass().getName() + " got: " + serviceByName.getClass().getName()); } } System.out.println("Test PASSED"); }
public static void main(String[] args) throws Exception { job = PrinterJob.getPrinterJob(); PrintService prtSrv = job.getPrintService(); if (job.getPrintService() == null) { System.out.println("No printers. Test cannot continue"); return; } if (!prtSrv.isAttributeCategorySupported(JobSheets.class)) { return; } SwingUtilities.invokeAndWait(() -> { doTest(BannerTest::printTest); }); mainThread = Thread.currentThread(); try { Thread.sleep(180000); } catch (InterruptedException e) { if (!testPassed && testGeneratedInterrupt) { throw new RuntimeException("Banner page did not print"); } } if (!testGeneratedInterrupt) { throw new RuntimeException("user has not executed the test"); } }
public static void main(String[] args) { PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); for(final PrintService service: services) { Thread thread = new Thread() { public void run() { service.getSupportedAttributeValues(Media.class, null, null); } }; thread.start(); } }
public static void main (String[] args) throws Exception { job = PrinterJob.getPrinterJob(); PrintService prtSrv = job.getPrintService(); if (prtSrv == null) { System.out.println("No printers. Test cannot continue"); return; } // do not run the test if JobSheet category is not supported if (!prtSrv.isAttributeCategorySupported(JobSheets.class)) { return; } // check system default banner option and let user know what to expect JobSheets js = (JobSheets)job.getPrintService(). getDefaultAttributeValue(JobSheets.class); if (js != null && js.equals(JobSheets.NONE)) { noJobSheet = true; } SwingUtilities.invokeAndWait(() -> { doTest(TestCheckSystemDefaultBannerOption::printTest); }); mainThread = Thread.currentThread(); try { Thread.sleep(60000); } catch (InterruptedException e) { if (!testPassed && testGeneratedInterrupt) { String banner = noJobSheet ? "Banner page" : " No Banner page"; throw new RuntimeException(banner + " is printed"); } } if (!testGeneratedInterrupt) { throw new RuntimeException("user has not executed the test"); } }
public static void doPrinterJob(String fileStr, OrientationRequested o) { PrinterJob pj = PrinterJob.getPrinterJob(); PrintService ps = pj.getPrintService(); if (ps == null) { System.out.println("No print service found."); return; } pj.setPrintable(new PrintToDir()); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(o); File f = new File(fileStr); // f.deleteOnExit(); URI dest = f.toURI(); Destination d = new Destination(dest); if (ps.isAttributeValueSupported(d, null, null)) { aset.add(d); try { pj.print(aset); } catch (PrinterException e) { System.out.println("PrinterJob passed."); return; } throw new RuntimeException("PrinterJob:PrinterException expected but not thrown. \nTEST FAILED"); } else { System.out.println("Destination attribute is not a supported value. PrinterJob passed."); } }
/** * Associate this PrinterJob with a new PrintService. * * Throws <code>PrinterException</code> if the specified service * cannot support the <code>Pageable</code> and * <code>Printable</code> interfaces necessary to support 2D printing. * @param a print service which supports 2D printing. * * @throws PrinterException if the specified service does not support * 2D printing or no longer available. */ public void setPrintService(PrintService service) throws PrinterException { if (service == null) { throw new PrinterException("Service cannot be null"); } else if (!(service instanceof StreamPrintService) && service.getName() == null) { throw new PrinterException("Null PrintService name."); } else { // Check the list of services. This service may have been // deleted already PrinterState prnState = (PrinterState)service.getAttribute( PrinterState.class); if (prnState == PrinterState.STOPPED) { PrinterStateReasons prnStateReasons = (PrinterStateReasons)service.getAttribute( PrinterStateReasons.class); if ((prnStateReasons != null) && (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))) { throw new PrinterException("PrintService is no longer available."); } } if (service.isDocFlavorSupported( DocFlavor.SERVICE_FORMATTED.PAGEABLE) && service.isDocFlavorSupported( DocFlavor.SERVICE_FORMATTED.PRINTABLE)) { myService = service; } else { throw new PrinterException("Not a 2D print service: " + service); } } }
protected boolean isSupportedValue(Attribute attrval, PrintRequestAttributeSet attrset) { PrintService ps = getPrintService(); return (attrval != null && ps != null && ps.isAttributeValueSupported(attrval, DocFlavor.SERVICE_FORMATTED.PAGEABLE, attrset)); }
synchronized PrinterGraphicsConfig getPrinterGraphicsConfig() { if (pgConfig != null) { return pgConfig; } String deviceID = "Printer Device"; PrintService service = getPrintService(); if (service != null) { deviceID = service.toString(); } pgConfig = new PrinterGraphicsConfig(deviceID, defaultDeviceTransform, deviceWidth, deviceHeight); return pgConfig; }
ServiceNotifier(PrintService service) { super(service.getName() + " notifier"); this.service = service; listeners = new Vector(); try { setPriority(Thread.NORM_PRIORITY-1); setDaemon(true); start(); } catch (SecurityException e) { } }
public void pageableJob(Pageable pageable) throws PrintException { try { synchronized(this) { if (job != null) { // shouldn't happen throw new PrintException("already printing"); } else { job = new sun.awt.windows.WPrinterJob(); } } PrintService svc = getPrintService(); job.setPrintService(svc); if (copies == 0) { Copies c = (Copies)svc.getDefaultAttributeValue(Copies.class); copies = c.getValue(); } job.setCopies(copies); job.setJobName(jobName); job.setPageable(pageable); job.print(reqAttrSet); notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE); return; } catch (PrinterException pe) { notifyEvent(PrintJobEvent.JOB_FAILED); throw new PrintException(pe); } finally { printReturned = true; notifyEvent(PrintJobEvent.NO_MORE_EVENTS); } }
public void run() { HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(new Copies(2)); // silly request but cuts out fax, xps, etc. PrintService[] printServices = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PRINTABLE, aset); for (PrintService ps : printServices) { log.log(Level.INFO, "Found printer: {0}", ps); printers.addItem(ps); if (ps.getName().equals(Prefs.getDefaultPrinter())) printers.setSelectedItem(ps); } }
/** * Invoked by the RasterPrintJob super class * this method is called after that last page * has been imaged. */ protected void endDoc() throws PrinterException { if (mPSStream != null) { mPSStream.println(EOF_COMMENT); mPSStream.flush(); if (mPSStream.checkError()) { abortDoc(); throw new PrinterException("Error while writing to file"); } if (mDestType != RasterPrinterJob.STREAM) { mPSStream.close(); } } if (mDestType == RasterPrinterJob.PRINTER) { PrintService pServ = getPrintService(); if (pServ != null) { mDestination = pServ.getName(); if (isMac) { PrintServiceAttributeSet psaSet = pServ.getAttributes(); if (psaSet != null) { mDestination = psaSet.get(PrinterName.class).toString() ; } } } PrinterSpooler spooler = new PrinterSpooler(); java.security.AccessController.doPrivileged(spooler); if (spooler.pex != null) { throw spooler.pex; } } }
private PrintService getServiceByName(PrinterName nameAttr) { String name = nameAttr.getValue(); if (name == null || name.equals("") || !checkPrinterName(name)) { return null; } /* check if all printers are already available */ if (printServices != null) { for (PrintService printService : printServices) { PrinterName printerName = printService.getAttribute(PrinterName.class); if (printerName.getValue().equals(name)) { return printService; } } } /* take CUPS into account first */ if (CUPSPrinter.isCupsRunning()) { try { return new IPPPrintService(name, new URL("http://"+ CUPSPrinter.getServer()+":"+ CUPSPrinter.getPort()+"/"+ name)); } catch (Exception e) { IPPPrintService.debug_println(debugPrefix+ " getServiceByName Exception "+ e); } } /* fallback if nothing not having a printer at this point */ PrintService printer = null; if (isMac() || isSysV()) { printer = getNamedPrinterNameSysV(name); } else if (isAIX()) { printer = getNamedPrinterNameAIX(name); } else { printer = getNamedPrinterNameBSD(name); } return printer; }
public synchronized PrintService[] getPrintServices() { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPrintJobAccess(); } if (printServices == null) { refreshServices(); } return printServices; }
UnixPrintJob(PrintService service) { this.service = service; mDestination = service.getName(); if (PrintServiceLookupProvider.isMac()) { mDestination = ((IPPPrintService)service).getDest(); } mDestType = UnixPrintJob.DESTPRINTER; JobSheets js = (JobSheets)(service. getDefaultAttributeValue(JobSheets.class)); if (js != null && js.equals(JobSheets.NONE)) { mNoJobSheet = true; } }
boolean matchingService(PrintService service, PrintServiceAttributeSet serviceSet) { if (serviceSet != null) { Attribute [] attrs = serviceSet.toArray(); Attribute serviceAttr; for (int i=0; i<attrs.length; i++) { serviceAttr = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory()); if (serviceAttr == null || !serviceAttr.equals(attrs[i])) { return false; } } } return true; }
public synchronized PrintService getDefaultPrintService() { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPrintJobAccess(); } // Windows does not have notification for a change in default // so we always get the latest. defaultPrinter = getDefaultPrinterName(); if (defaultPrinter == null) { return null; } if ((defaultPrintService != null) && defaultPrintService.getName().equals(defaultPrinter)) { return defaultPrintService; } // Not the same as default so proceed to get new PrintService. // clear defaultPrintService defaultPrintService = null; if (printServices != null) { for (int j=0; j<printServices.length; j++) { if (defaultPrinter.equals(printServices[j].getName())) { defaultPrintService = printServices[j]; break; } } } if (defaultPrintService == null) { defaultPrintService = new Win32PrintService(defaultPrinter); } return defaultPrintService; }
public PrintRequestAttributeSet showDocumentProperties(PrinterJob job, Window owner, PrintService service, PrintRequestAttributeSet aset) { if (!(job instanceof WPrinterJob)) { return null; } WPrinterJob wJob = (WPrinterJob)job; return wJob.showDocumentProperties(owner, service, aset); }
/** * Prompts the user to choose a printer to print from, using a standard dialog box. * * The user is also able to selected from other properties such as the number of copies to * print, collation, etc., independently of our software. * * @return The PrintService (a.k.a the printer) selected by the user. */ private static PrintService choosePrinter() { PrinterJob printJob = PrinterJob.getPrinterJob(); if(printJob.printDialog()) { return printJob.getPrintService(); } else { return null; } }
private PrintService getNamedPrinterNameSysV(String name) { String command = "/usr/bin/lpstat -v " + name; String []result = execCmd(command); if (result == null || result[0].indexOf("unknown printer") > 0) { return null; } else { return new UnixPrintService(name); } }
static void trial() { PrintService pserv1 = PrintServiceLookup.lookupDefaultPrintService(); PrintService[] pservs = PrintServiceLookup.lookupPrintServices(null, null); PrintService pserv2 = PrintServiceLookup.lookupDefaultPrintService(); if ((pserv1 == null) || (pserv2==null)) { return; } if (pserv1.hashCode() != pserv2.hashCode()) { throw new RuntimeException("Different hashCodes for equal print " + "services: " + pserv1.hashCode() + " " + pserv2.hashCode()); } }
UnixPrintJob(PrintService service) { this.service = service; mDestination = service.getName(); if (UnixPrintServiceLookup.isMac()) { mDestination = ((IPPPrintService)service).getDest(); } mDestType = UnixPrintJob.DESTPRINTER; }
public synchronized PrintService[] getPrintServices() { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPrintJobAccess(); } if (printServices == null || !pollServices) { refreshServices(); } if (printServices == null) { return new PrintService[0]; } else { return (PrintService[])printServices.clone(); } }
public static void main(String[] args) throws Exception { for (PrintService service : PrintServiceLookup.lookupPrintServices(null, null)) { String serviceName = service.getName(); PrintService serviceByName = lookupByName(serviceName); if (!service.equals(serviceByName)) { throw new RuntimeException("NOK " + serviceName + " expected: " + service.getClass().getName() + " got: " + serviceByName.getClass().getName()); } } System.out.println("Test PASSED"); }
private PrintService getServiceByName(PrinterName nameAttr) { String name = nameAttr.getValue(); if (name == null || name.equals("") || !checkPrinterName(name)) { return null; } /* check if all printers are already available */ if (printServices != null) { for (PrintService printService : printServices) { PrinterName printerName = (PrinterName)printService.getAttribute(PrinterName.class); if (printerName.getValue().equals(name)) { return printService; } } } /* take CUPS into account first */ if (CUPSPrinter.isCupsRunning()) { try { return new IPPPrintService(name, new URL("http://"+ CUPSPrinter.getServer()+":"+ CUPSPrinter.getPort()+"/"+ name)); } catch (Exception e) { IPPPrintService.debug_println(debugPrefix+ " getServiceByName Exception "+ e); } } /* fallback if nothing not having a printer at this point */ PrintService printer = null; if (isMac() || isSysV()) { printer = getNamedPrinterNameSysV(name); } else if (isAIX()) { printer = getNamedPrinterNameAIX(name); } else { printer = getNamedPrinterNameBSD(name); } return printer; }
public static void main(String[] args) throws Exception { String os = System.getProperty("os.name").toLowerCase(); System.out.println("OS is " + os); if (!os.equals("linux")) { System.out.println("Linux specific test. No need to continue"); return; } PrintService services[] = PrintServiceLookup.lookupPrintServices(null, null); if (services.length > 0) { System.out.println("Services found. No need to test further."); return; } String[] lpcmd = { "lpstat", "-a" }; Process proc = Runtime.getRuntime().exec(lpcmd); proc.waitFor(); InputStreamReader ir = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader(ir); int count = 0; String printer; while ((printer = br.readLine()) != null) { System.out.println("lpstat:: " + printer); count++; } if (count > 0) { throw new RuntimeException("Services exist, but not found by JDK."); } }
private PrintService getNamedPrinterNameBSD(String name) { if (cmdIndex == UNINITIALIZED) { cmdIndex = getBSDCommandIndex(); } String command = "/usr/sbin/lpc status " + name + lpcNameCom[cmdIndex]; String[] result = execCmd(command); if (result == null || !(result[0].equals(name))) { return null; } return new UnixPrintService(name); }