public static void saveComponentAsEPS(Component c) throws IOException { // Find a factory object for printing Printable objects to PostScript. DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; String format = "application/postscript"; //$NON-NLS-1$ StreamPrintServiceFactory factory = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, format)[0]; // Ask the user to select a file and open the selected file //JFileChooser chooser = new JFileChooser(); JFileChooser chooser = OSPRuntime.getChooser(); if(chooser.showSaveDialog(c)!=JFileChooser.APPROVE_OPTION) { return; } File f = chooser.getSelectedFile(); FileOutputStream out = new FileOutputStream(f); // Obtain a PrintService that prints to that file StreamPrintService service = factory.getPrintService(out); // Do the printing with the method below printToService(c, service, null); // And close the output file. out.close(); }
/** * 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); } } }
public static void main(String[] args) throws Exception { GraphicsEnvironment.getLocalGraphicsEnvironment(); DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; String mime = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType(); StreamPrintServiceFactory[] factories = StreamPrintServiceFactory. lookupStreamPrintServiceFactories(flavor, mime); if (factories.length == 0) { System.out.println("No print service found."); return; } FileOutputStream output = new FileOutputStream("out.ps"); StreamPrintService service = factories[0].getPrintService(output); SimpleDoc doc = new SimpleDoc(new PrintSEUmlauts(), DocFlavor.SERVICE_FORMATTED.PRINTABLE, new HashDocAttributeSet()); DocPrintJob job = service.createPrintJob(); job.addPrintJobListener(new PrintJobAdapter() { @Override public void printJobCompleted(PrintJobEvent pje) { testPrintAndExit(); } }); job.print(doc, new HashPrintRequestAttributeSet()); }
/** * Associate this PrinterJob with a new PrintService. * * Throws {@code PrinterException} if the specified service * cannot support the {@code Pageable} and * {@code Printable} interfaces necessary to support 2D printing. * @param service 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 = service.getAttribute(PrinterState.class); if (prnState == PrinterState.STOPPED) { PrinterStateReasons prnStateReasons = 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); } } }
public void print() throws IOException, PrintException { DocFlavor inputFlavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8; // Lookup a print factory to convert from desired input to output. StreamPrintServiceFactory[] psfactories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories( inputFlavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType()); if (psfactories.length == 0) { System.err.println("Ack! No StreamPrintFactory found for this job!"); } StreamPrintService printService = psfactories[0].getPrintService(new FileOutputStream("demo.ps")); PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); attrs.add(OrientationRequested.LANDSCAPE); attrs.add(MediaSizeName.NA_LETTER); attrs.add(new Copies(1)); attrs.add(new JobName(INPUT_FILE_NAME, null)); InputStream is = getClass().getResourceAsStream(INPUT_FILE_NAME); if (is == null) { throw new NullPointerException( "Input Stream is null: file not found?"); } Doc doc = new SimpleDoc(is, inputFlavor, null); DocPrintJob printJob = printService.createPrintJob(); printJob.print(doc, attrs); }
/** * Write PostScript to writer * * @param os output stream * @return true if success */ public boolean createPS(OutputStream os) { try { String outputMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType(); DocFlavor docFlavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE; StreamPrintServiceFactory[] spsfactories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(docFlavor, outputMimeType); if (spsfactories.length == 0) { log.error("(fos) - No StreamPrintService"); return false; } // just use first one - sun.print.PSStreamPrinterFactory // System.out.println("- " + spsfactories[0]); StreamPrintService sps = spsfactories[0].getPrintService(os); // get format if (m_layout == null) layout(); // print it sps.createPrintJob().print(m_layout.getPageable(false), new HashPrintRequestAttributeSet()); // os.flush(); // following 2 line for backward compatibility if (os instanceof FileOutputStream) ((FileOutputStream)os).close(); } catch (Exception e) { log.error("(fos)", e); } return false; }
protected All2PSDocPrintJob(StreamPrintService printService) { super(); begetPrintService = printService; printJobListeners = new ArrayList(); printJobAttributeListeners = new ArrayList(); printJobAttributeSet = new HashPrintJobAttributeSet(); jobName = "PS printing"; copies = 1; outstream = new PrintStream(printService.getOutputStream()); }
/** * job - parent DefaultPrintJob doc - doc to print attributeset - * attributes set spsDocPrintJob - stream print service's print job */ PrintJobThread(DefaultPrintJob job, Doc doc, PrintRequestAttributeSet attributeset, StreamPrintService sps) { this.printJob = job; this.printDoc = doc; this.printAttributeSet = attributeset; this.streamservice = sps; this.exception = null; this.exceptionisnotnull = false; }
public StreamPrintService getPrintService(OutputStream out) { return new PSStreamPrintService(out); }
/** * Display a dialog to the user allowing the modification of a * PageFormat instance. * The <code>page</code> argument is used to initialize controls * in the page setup dialog. * If the user cancels the dialog, then the method returns the * original <code>page</code> object unmodified. * If the user okays the dialog then the method returns a new * PageFormat object with the indicated changes. * In either case the original <code>page</code> object will * not be modified. * @param page the default PageFormat presented to the user * for modification * @return the original <code>page</code> object if the dialog * is cancelled, or a new PageFormat object containing * the format indicated by the user if the dialog is * acknowledged * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @since JDK1.2 */ public PageFormat pageDialog(PageFormat page) throws HeadlessException { if (GraphicsEnvironment.isHeadless()) { throw new HeadlessException(); } if (getPrintService() instanceof StreamPrintService) { return super.pageDialog(page); } PageFormat pageClone = (PageFormat) page.clone(); boolean result = false; /* * Fix for 4507585: show the native modal dialog the same way printDialog() does so * that it won't block event dispatching when called on EventDispatchThread. */ WPageDialog dialog = new WPageDialog((Frame)null, this, pageClone, null); dialog.setRetVal(false); dialog.setVisible(true); result = dialog.getRetVal(); dialog.dispose(); // myService => current PrintService if (result && (myService != null)) { // It's possible that current printer is changed through // the "Printer..." button so we query again from native. String printerName = getNativePrintService(); if (!myService.getName().equals(printerName)) { // native printer is different ! // we update the current PrintService try { setPrintService(Win32PrintServiceLookup. getWin32PrintLUS(). getPrintServiceByName(printerName)); } catch (PrinterException e) { } } // Update attributes, this will preserve the page settings. // - same code as in RasterPrinterJob.java updatePageAttributes(myService, pageClone); return pageClone; } else { return page; } }
private void createChartPrintPostScriptJob() { // Use the pre-defined flavor for a Printable from an InputStream DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; // Specify the type of the output stream String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType(); // Locate factory which can export a GIF image stream as Postscript StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories( flavor, psMimeType); if (factories.length == 0) { System.err.println("No suitable factories"); System.exit(0); // FIXME too } // Obtain file name from user JFileChooser fileChooser = new JFileChooser(); ExtensionFileFilter filter = new ExtensionFileFilter( localizationResources.getString("PostScript_Files"), ".eps"); fileChooser.addChoosableFileFilter(filter); String filename=""; int option = fileChooser.showSaveDialog(this); if (option == JFileChooser.APPROVE_OPTION) { filename = fileChooser.getSelectedFile().getPath(); if (isEnforceFileExtensions()) { if (!filename.endsWith(".eps")) { filename = filename + ".eps"; } } else return; } try { // Create a file for the exported postscript FileOutputStream fos = new FileOutputStream(filename); // Create a Stream printer for Postscript StreamPrintService sps = factories[0].getPrintService(fos); // Create and call a Print Job DocPrintJob pj = sps.createPrintJob(); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); Doc doc = new SimpleDoc(this, flavor, null); pj.print(doc, aset); fos.close(); } catch (PrintException pe) { System.err.println(pe); } catch (IOException ie) { System.err.println(ie); } }