public Print2DPrinterJob() { /* Construct the print request specification. * The print data is a Printable object. * the request additonally specifies a job name, 2 copies, and * landscape orientation of the media. */ PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(OrientationRequested.LANDSCAPE); aset.add(new Copies(2)); aset.add(new JobName("My job", null)); /* Create a print job */ PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintable(this); /* locate a print service that can handle the request */ PrintService[] services = PrinterJob.lookupPrintServices(); if (services.length > 0) { System.out.println("selected printer " + services[0].getName()); try { pj.setPrintService(services[0]); pj.pageDialog(aset); if(pj.printDialog(aset)) { pj.print(aset); } } catch (PrinterException pe) { System.err.println(pe); } } }
/** * Show a dialog for printing the current drawing. * * @param fff the parent frame which will be used for dialogs and message boxes. * @param CCr the CircuitPanel containing the drawing to be exported. */ public void printDrawing(JFrame fff, CircuitPanel CCr) { cc = CCr; DialogPrint dp = new DialogPrint(fff); dp.setMirror(printMirror); dp.setFit(printFitToPage); dp.setBW(printBlackWhite); dp.setLandscape(printLandscape); dp.setVisible(true); // Get some information about the printing options. printMirror = dp.getMirror(); printFitToPage = dp.getFit(); printLandscape = dp.getLandscape(); printBlackWhite = dp.getBW(); Vector<LayerDesc> ol = cc.dmp.getLayers(); if (dp.shouldPrint()) { if (printBlackWhite) { Vector<LayerDesc> v = new Vector<LayerDesc>(); // Here we create an alternative array of layers in // which all colors are pitch black. This may be // useful for PCB's. for (int i = 0; i < LayerDesc.MAX_LAYERS; ++i) v.add( new LayerDesc( new ColorSwing(Color.black), ((LayerDesc) ol.get(i)).getVisible(), "B/W", ((LayerDesc) ol.get(i)).getAlpha())); cc.dmp.setLayers(v); } PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); // Set the correct printing orientation. if (printLandscape) { aset.add(OrientationRequested.LANDSCAPE); } else { aset.add(OrientationRequested.PORTRAIT); } job.print(aset); } catch (PrinterException ex) { // The job did not successfully complete JOptionPane.showMessageDialog(fff, Globals.messages.getString("Print_uncomplete")); } } cc.dmp.setLayers(ol); } }
private void printPlot() { PrinterJob job = PrinterJob.getPrinterJob(); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); // PageFormat pf = job.pageDialog(aset); job.setPrintable(this); boolean ok = job.printDialog(aset); if (ok) { try { job.print(aset); } catch (PrinterException ex) { // showFeedback("An error was encountered while printing." + ex); /* The job did not successfully complete */ } } }
// {{{ getPrintJob() method private static PrinterJob getPrintJob(String jobName) { job = PrinterJob.getPrinterJob(); format = new HashPrintRequestAttributeSet(); String settings = jEdit.getSettingsDirectory(); if (settings != null) { String printSpecPath = MiscUtilities.constructPath(settings, "printspec"); File filePrintSpec = new File(printSpecPath); if (filePrintSpec.exists()) { try { FileInputStream fileIn = new FileInputStream(filePrintSpec); ObjectInputStream obIn = new ObjectInputStream(fileIn); format = (HashPrintRequestAttributeSet) obIn.readObject(); } catch (Exception e) { Log.log(Log.ERROR, BufferPrinter1_4.class, e); } // for backwards compatibility, the color variable is stored also as a property if (jEdit.getBooleanProperty("print.color")) format.add(Chromaticity.COLOR); else format.add(Chromaticity.MONOCHROME); // no need to always keep the same job name for every printout. format.add(new JobName(jobName, null)); } } return job; } // }}}
// {{{ getPageFormat() method public static PageFormat getPageFormat() { // convert from PrintRequestAttributeSet to the pageFormat PrinterJob prnJob = getPrintJob(" "); PageFormat pf = prnJob.defaultPage(); Paper pap = pf.getPaper(); MediaSizeName media = (MediaSizeName) format.get(Media.class); MediaSize ms = MediaSize.getMediaSizeForName(media); MediaPrintableArea mediaarea = (MediaPrintableArea) format.get(MediaPrintableArea.class); if (mediaarea != null) pap.setImageableArea( (mediaarea.getX(MediaPrintableArea.INCH) * 72), (mediaarea.getY(MediaPrintableArea.INCH) * 72), (mediaarea.getWidth(MediaPrintableArea.INCH) * 72), (mediaarea.getHeight(MediaPrintableArea.INCH) * 72)); if (ms != null) pap.setSize((ms.getX(MediaSize.INCH) * 72), (ms.getY(MediaSize.INCH) * 72)); pf.setPaper(pap); OrientationRequested orientation = (OrientationRequested) format.get(OrientationRequested.class); if (orientation != null) { if (orientation.getValue() == OrientationRequested.LANDSCAPE.getValue()) { pf.setOrientation(PageFormat.LANDSCAPE); } else if (orientation.getValue() == OrientationRequested.REVERSE_LANDSCAPE.getValue()) { pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); } else if (orientation.getValue() == OrientationRequested.PORTRAIT.getValue()) { pf.setOrientation(PageFormat.PORTRAIT); } else if (orientation.getValue() == OrientationRequested.REVERSE_PORTRAIT.getValue()) { // doesnt exist?? // pf.setOrientation(PageFormat.REVERSE_PORTRAIT); // then just do the next best thing pf.setOrientation(PageFormat.PORTRAIT); } } return pf; } // }}}
// {{{ pageSetup() method public static void pageSetup(View view) { PrinterJob prnJob = getPrintJob("PageSetupOnly"); if (prnJob.pageDialog(format) != null) savePrintSpec(); } // }}}