DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); PrintService[] printerArray = dialogPrinters.toArray(new PrintService[dialogPrinters.size()]); // call print dialog and get print attributes PrintService selectedPrinter = javax.print.ServiceUI.printDialog(null, 200, 200, printerArray, defaultPrintService, flavor, pras); // check if "print-to-file" option used if (pras.get(Destination.class) != null) { // here we deny to perform the save into a file JOptionPane.showMessageDialog(CMSJRViewer.this, getBundleString("error.printing")); throw new PrintException("Print to file option not allowed. Action aborted!"); } else { ... }
/** * Utility for creating a print setup dialog. * * @return print service selected by the user, or null if the user * cancelled the dialog. */ private PrintService getSetupDialog() { final int offset = 50; // find graphic configuration for the window the viewer is in. Window window = SwingUtilities.getWindowAncestor( container); GraphicsConfiguration graphicsConfiguration = window == null ? null : window.getGraphicsConfiguration(); // try and trim the services list. // services = new PrintService[]{services[0]}; return ServiceUI.printDialog(graphicsConfiguration, container.getX() + offset, container.getY() + offset, services, services[0], DocFlavor.SERVICE_FORMATTED.PRINTABLE, printRequestAttributeSet); }
private void printHelloDocument(String value) { PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); DocFlavor flavor = DocFlavor.READER.TEXT_PLAIN; Doc doc = new SimpleDoc(new StringReader(value), flavor, null); PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, aset); PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); if (services.length == 0) { if (defaultService == null) { new Notification("No printer found", WARNING_MESSAGE).show(getPage()); } else { DocPrintJob job = defaultService.createPrintJob(); printDocument(doc, aset, job); } } else { SwingUtilities.invokeLater(() -> { PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, aset); if (service != null) { DocPrintJob job = service.createPrintJob(); printDocument(doc, aset, job); } }); } }
PrintService chosenService = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, attributeSet); if (chosenService != null) {