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; }
public static void main(String[] args) throws Exception { prservices = PrintServiceLookup.lookupDefaultPrintService(); if (prservices == null) { System.out.println("No print service found"); return; } System.out.println(" Print service " + prservices); SwingUtilities.invokeAndWait(() -> { doTest(TestMediaTraySelection::printTest); }); mainThread = Thread.currentThread(); try { Thread.sleep(90000); } 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"); } }
/** * 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]); } }
/** * Starts the application. */ public static void main(java.lang.String[] args) throws Exception { services = PrintServiceLookup.lookupPrintServices(flavor, null); if (services.length == 0) { System.out.println("No print service found!!"); return; } SwingUtilities.invokeAndWait(() -> { doTest(ServiceDlgPageRangeTest::printTest); }); mainThread = Thread.currentThread(); try { Thread.sleep(600000); } catch (InterruptedException e) { if (!testPassed && testGeneratedInterrupt) { throw new RuntimeException("PageRanges option is not disabled " + "for for Non serv-formatted flvrs"); } } if (!testGeneratedInterrupt) { throw new RuntimeException("user has not executed the test"); } }
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 PrintService getPrintService(String printername) { // Initalize print service if (printername == null) { return PrintServiceLookup.lookupDefaultPrintService(); } else { if ("(Show dialog)".equals(printername)) { return null; // null means "you have to show the print dialog" } else if ("(Default)".equals(printername)) { return PrintServiceLookup.lookupDefaultPrintService(); } else { PrintService[] pservices = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PRINTABLE , null); for (PrintService s : pservices) { if (printername.equals(s.getName())) { return s; } } return PrintServiceLookup.lookupDefaultPrintService(); } } }
protected void setupJavaPrint() { // "install" another default printer PrintService psDefault = mock(PrintService.class); when(psDefault.getName()).thenReturn("DefaultPrinter"); when(psDefault.isDocFlavorSupported(any(DocFlavor.class))).thenReturn(Boolean.TRUE); PrintServiceLookup psLookup = mock(PrintServiceLookup.class); when(psLookup.getPrintServices()).thenReturn(new PrintService[]{psDefault}); when(psLookup.getDefaultPrintService()).thenReturn(psDefault); DocPrintJob docPrintJob = mock(DocPrintJob.class); when(psDefault.createPrintJob()).thenReturn(docPrintJob); MediaTray[] trays = new MediaTray[]{ MediaTray.TOP, MediaTray.MIDDLE, MediaTray.BOTTOM }; when(psDefault.getSupportedAttributeValues(Media.class, null, null)).thenReturn(trays); PrintServiceLookup.registerServiceProvider(psLookup); }
/** * * @param printername * @return */ public static PrintService getPrintService(String printername) { // Initalize print service if (printername == null) { return PrintServiceLookup.lookupDefaultPrintService(); } else { if ("(Show dialog)".equals(printername)) { return null; // null means "you have to show the print dialog" } else if ("(Default)".equals(printername)) { return PrintServiceLookup.lookupDefaultPrintService(); } else { PrintService[] pservices = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PRINTABLE , null); for (PrintService s : pservices) { if (printername.equals(s.getName())) { return s; } } return PrintServiceLookup.lookupDefaultPrintService(); } } }
@Cached /* package */PrintService getSystemPrintService(final String printerName) { final PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); if (services == null || services.length == 0) { return null; } for (final PrintService service : services) { if (service.getName().equals(printerName)) { return service; } } return null; }
@Override public void create(String filename) { // find the printing service //http://www.coderanch.com/t/385989/java/java/send-raw-data-printer AttributeSet attributeSet = new HashAttributeSet(); attributeSet.add(new PrinterName(filename, null)); PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributeSet); if(services.length > 0){ if(printFile != null){ printFile.deleteOnExit(); printFile = null; } try { printFile = File.createTempFile("printer_"+name,".redirect"); } catch (IOException e) { e.printStackTrace(); } printService = services[0]; } }
public PrintService getPrintService() { if (myService == null) { String printerName = getNativePrintService(); if (printerName != null) { myService = Win32PrintServiceLookup.getWin32PrintLUS(). getPrintServiceByName(printerName); // no need to call setNativePrintService as this name is // currently set in native if (myService != null) { return myService; } } myService = PrintServiceLookup.lookupDefaultPrintService(); if (myService != null) { try { setNativePrintService(myService.getName()); } catch (Exception e) { myService = null; } } } return myService; }
public DruckerController(DruckerModel model, JFrame parent, MailMergeParams mmp) { this.mmp = mmp; this.model = model; this.parent = parent; model.setDrucker( PrintParametersDialog.getCurrentPrinterName( mmp.getMMC().getTextDocument())); PrintService[] printservices = PrintServiceLookup.lookupPrintServices(null, null); for ( PrintService service : printservices) { alleDrucker.add(service.getName()); } model.setAlleDrucker(alleDrucker); }
public static boolean out(BufferedImage image) { try { DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; PrintRequestAttributeSet requestAttributeSet = new HashPrintRequestAttributeSet(); requestAttributeSet.add(MediaSizeName.ISO_A4); requestAttributeSet.add(new JobName(LSystem.applicationName + LSystem.getTime(), Locale.ENGLISH)); PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, requestAttributeSet); PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); PrintService service = ServiceUI.printDialog(null, 100, 100, services, defaultService, flavor, requestAttributeSet); if (service != null) { DocPrintJob job = service.createPrintJob(); SimpleDoc doc = new SimpleDoc(new BufferedImagePrintable(image), flavor, null); job.print(doc, requestAttributeSet); } } catch (Exception e) { return false; } return true; }
@Test public void testListPrinterTrays () { final PrintService [] aAllServices = PrintServiceLookup.lookupPrintServices (null, null); for (final PrintService aService : aAllServices) { s_aLogger.info (aService.toString ()); final Object aAttrs = aService.getSupportedAttributeValues (Media.class, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); if (ArrayHelper.isArray (aAttrs)) { for (final Media aElement : (Media []) aAttrs) if (aElement instanceof MediaTray) s_aLogger.info (" " + aElement); } } }
public PrintService getPrintService() { if (myService == null) { String printerName = getNativePrintService(); if (printerName != null) { myService = Win32PrintServiceLookup.getWin32PrintLUS(). getPrintServiceByName(printerName); // no need to call setNativePrintService as this name is // currently set in native if (myService != null) { return myService; } } myService = PrintServiceLookup.lookupDefaultPrintService(); if (myService instanceof Win32PrintService) { try { setNativePrintServiceIfNeeded(myService.getName()); } catch (Exception e) { myService = null; } } } return myService; }
private int pageSetup(PrintService aService, PrintRequestAttributeSet requestAttrs) { myService = (aService == null) ? PrintServiceLookup.lookupDefaultPrintService() : aService; if ((requestAttrs == null) || (aService == null)) { return SETUP_ERROR; } attrs = requestAttrs; newAttrs = new HashPrintRequestAttributeSet(attrs); myService = aService; prepareDialog(); // prepare dialog fillPageSetupFields(); // Initialize dialog fields firstUse = false; return SETUP_OK; }
/** * Function to print code Zpl to local zebra(usb) * * @param zpl * code Zpl to print * @param ip * ip adress * @param port * port * @throws ZebraPrintException * if zpl could not be printed */ public static void printZpl(String zpl, String printerName) throws ZebraPrintException { try { PrintService psZebra = null; String sPrinterName = null; PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); for (int i = 0; i < services.length; i++) { PrintServiceAttribute attr = services[i].getAttribute(PrinterName.class); sPrinterName = ((PrinterName) attr).getValue(); if (sPrinterName.toLowerCase().indexOf(printerName) >= 0) { psZebra = services[i]; break; } } if (psZebra == null) { throw new ZebraPrintNotFoundException("Zebra printer not found : " + printerName); } DocPrintJob job = psZebra.createPrintJob(); byte[] by = zpl.getBytes(); DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; Doc doc = new SimpleDoc(by, flavor, null); job.print(doc, null); } catch (PrintException e) { throw new ZebraPrintException("Cannot print label on this printer : " + printerName, e); } }
public PrintService getPrintService() { if (myService == null) { String printerName = getNativePrintService(); if (printerName != null) { myService = Win32PrintServiceLookup.getWin32PrintLUS(). getPrintServiceByName(printerName); // no need to call setNativePrintService as this name is // currently set in native if (myService != null) { return myService; } } myService = PrintServiceLookup.lookupDefaultPrintService(); if (myService != null) { try { setNativePrintServiceIfNeeded(myService.getName()); } catch (Exception e) { myService = null; } } } return myService; }
/** * Find all the printer resources on the server * * @return */ public static List findPrinters( ) { List printers = new ArrayList( ); PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet( ); DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT; PrintService[] printServices = PrintServiceLookup.lookupPrintServices( flavor, pras ); if ( printServices != null ) { for ( int i = 0; i < printServices.length; i++ ) { PrintService service = printServices[i]; printers.add( createPrinter( service ) ); } } return printers; }
@Before public void setup() { PrintService psDefault = Mockito.mock(PrintService.class); Mockito.when(psDefault.getName()).thenReturn("DefaultPrinter"); Mockito.when(psDefault.isDocFlavorSupported(Mockito.any(DocFlavor.class))).thenReturn(Boolean.TRUE); PrintServiceLookup psLookup = Mockito.mock(PrintServiceLookup.class); Mockito.when(psLookup.getPrintServices()).thenReturn(new PrintService[]{psDefault}); Mockito.when(psLookup.getDefaultPrintService()).thenReturn(psDefault); DocPrintJob docPrintJob = Mockito.mock(DocPrintJob.class); Mockito.when(psDefault.createPrintJob()).thenReturn(docPrintJob); MediaTray[] trays = new MediaTray[]{ MediaTray.TOP, MediaTray.MIDDLE, MediaTray.BOTTOM }; Mockito.when(psDefault.getSupportedAttributeValues(Media.class, null, null)).thenReturn(trays); PrintServiceLookup.registerServiceProvider(psLookup); }