public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex>0) { return NO_SUCH_PAGE; } StringBuffer s=new StringBuffer(); for (int i=0;i<10;i++) { s.append("1234567890ABCDEFGHIJ"); } int x=(int) pageFormat.getImageableX(); int y=(int) (pageFormat.getImageableY()+50); graphics.drawString(s.toString(), x, y); return PAGE_EXISTS; }
public int print(final Graphics graphics, final PageFormat pageFormat, final int pageIndex) throws PrinterException { final int retVal = printDelegatee.print(graphics, pageFormat, pageIndex); if (retVal != NO_SUCH_PAGE && !isAborted()) { if (SwingUtilities.isEventDispatchThread()) { updateStatusOnEDT(pageIndex); } else { SwingUtilities.invokeLater(new Runnable() { public void run() { updateStatusOnEDT(pageIndex); } }); } } return retVal; }
void print(List<Paper> papers) { PrinterJob job = PrinterJob.getPrinterJob(); myPapers = papers; //out("SET PAPER: " + myPapers); if (job == null) { return; } job.setPrintable(this, Config.getDefault().getPageFormat()); try { if (job.printDialog()) { job.print(); } } catch (PrinterException e) { printError(i18n(Printer.class, "ERR_Printer_Problem", e.getLocalizedMessage())); // NOI18N } myPapers = null; }
/** * Creates a print job for the chart. */ @Override public void createChartPrintJob() { PrinterJob job = PrinterJob.getPrinterJob(); PageFormat pf = job.defaultPage(); PageFormat pf2 = job.pageDialog(pf); if (pf2 != pf) { job.setPrintable(this, pf2); if (job.printDialog()) { try { job.print(); } catch (PrinterException e) { JOptionPane.showMessageDialog(this, e); } } } }
@Override protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException { super.setAttributes(attributes); if (attributes == null) { return; // now always use attributes, so this shouldn't happen. } Attribute attr = attributes.get(Media.class); if (attr instanceof CustomMediaTray) { CustomMediaTray customTray = (CustomMediaTray)attr; String choice = customTray.getChoiceName(); if (choice != null) { mOptions = " InputSlot="+ choice; } } }
public void pageDialogExample() throws PrinterException { PrinterJob job = PrinterJob.getPrinterJob(); PageFormat originalPageFormat = job.defaultPage(); PageFormat pageFormat = job.pageDialog(originalPageFormat); if(originalPageFormat == pageFormat) return; job.setPrintable(this,pageFormat); job.print(); }
@Override public int print(Graphics default_graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { List<Image> images = getPrintPages(pageFormat); if(pageIndex>=images.size()) return NO_SUCH_PAGE; Graphics2D graphics = (Graphics2D)default_graphics.create(); graphics.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); graphics.drawImage(images.get(pageIndex), 0, 0, null); Thread.yield(); //yield shortly, so that the image is painted return PAGE_EXISTS; }
@Override public int print(Graphics default_graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if(pageIndex!=0) return NO_SUCH_PAGE; pageFormat.setOrientation(PageFormat.LANDSCAPE); Color foreground = this.foreground, background = this.background, grid = this.grid; this.foreground = Color.BLACK; this.background = Color.WHITE; this.grid = Color.LIGHT_GRAY; Graphics2D graphics = (Graphics2D) default_graphics; graphics.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); this.paint(graphics); Thread.yield(); //yield shortly that the graphics are printed properly this.foreground = foreground; this.background = background; this.grid = grid; return PAGE_EXISTS; }
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 }
/** * Redraw a rectanglular area using a proxy graphics */ public abstract void redrawRegion(Rectangle2D region, double scaleX, double scaleY, Shape clip, AffineTransform devTransform) throws PrinterException ;
/** * Prints the page at the specified index into the specified * {@link Graphics} context in the specified * format. A <code>PrinterJob</code> calls the * <code>Printable</code> interface to request that a page be * rendered into the context specified by * <code>graphics</code>. The format of the page to be drawn is * specified by <code>pageFormat</code>. The zero based index * of the requested page is specified by <code>pageIndex</code>. * If the requested page does not exist then this method returns * NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned. * The <code>Graphics</code> class or subclass implements the * {@link PrinterGraphics} interface to provide additional * information. If the <code>Printable</code> object * aborts the print job then it throws a {@link PrinterException}. * @param graphics the context into which the page is drawn * @param pageFormat the size and orientation of the page being drawn * @param pageIndex the zero based index of the page to be drawn * @return PAGE_EXISTS if the page is rendered successfully * or NO_SUCH_PAGE if <code>pageIndex</code> specifies a * non-existent page. * @exception java.awt.print.PrinterException * thrown when the print job is terminated. */ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { int result; /* This method will be called by the PrinterJob on a thread other * that the application's thread. We hold on to the graphics * until we can rendevous with the application's thread and * hand over the graphics. The application then does all the * drawing. When the application is done drawing we rendevous * again with the PrinterJob thread and release the Graphics * so that it knows we are done. */ /* Add the graphics to the message queue of graphics to * be rendered. This is really a one slot queue. The * application's thread will come along and remove the * graphics from the queue when the app asks for a graphics. */ graphicsToBeDrawn.append( (Graphics2D) graphics); /* We now wait for the app's thread to finish drawing on * the Graphics. This thread will sleep until the application * release the graphics by placing it in the graphics drawn * message queue. If the application signals that it is * finished drawing the entire document then we'll get null * returned when we try and pop a finished graphic. */ if (graphicsDrawn.pop() != null) { result = PAGE_EXISTS; } else { result = NO_SUCH_PAGE; } return result; }
@Override public int print(Graphics g, PageFormat pf, int i) throws PrinterException { if (i > 0) return NO_SUCH_PAGE; Graphics2D g2 = (Graphics2D)g; // translate to printable area g2.translate(pf.getImageableX(), pf.getImageableY()); // now attempt to scale if needed double scale = Math.min(pf.getImageableWidth() / getWidth(), pf.getImageableHeight() / getHeight()); int width = getWidth(); if ((scale > 1.05) || (scale < 0.95)) { g2.scale(scale, scale); width *= scale; } // final translate to center of printable area after we know scaled size if (width < pf.getImageableWidth()) { int centerx = (int) ((pf.getImageableWidth() - width)/2.0); g2.translate(centerx, 0); } paint(g); return PAGE_EXISTS; }
private static void printWithJavaPrintDialog() { final JTable table = createAuthorTable(50); Printable printable = table.getPrintable( JTable.PrintMode.NORMAL, new MessageFormat("Author Table"), new MessageFormat("Page - {0}")); PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(printable); boolean printAccepted = job.printDialog(); if (printAccepted) { try { job.print(); closeFrame(); } catch (PrinterException e) { throw new RuntimeException(e); } } }
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { if (pageIndex > 0) { return Printable.NO_SUCH_PAGE; } g.translate((int) pf.getImageableX(), (int) pf.getImageableY()); g.setFont(new Font("Dialog", Font.PLAIN, 36)); g.drawString("\u4e00\u4e01\u4e02\u4e03\u4e04English", 20, 100); return Printable.PAGE_EXISTS; }
/** * Creates a print job for the chart. */ public void createChartPrintJob() { PrinterJob job = PrinterJob.getPrinterJob(); PageFormat pf = job.defaultPage(); PageFormat pf2 = job.pageDialog(pf); if (pf2 != pf) { job.setPrintable(this, pf2); if (job.printDialog()) { try { job.print(); } catch (PrinterException e) { JOptionPane.showMessageDialog(this, e); } } } }
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex != 0) { return NO_SUCH_PAGE; } /* CairoImage image = new CairoImage( this.getBounds().width, this.getBounds().height ); Graphics2D g2 = image.createGraphics2D(); double x = pageFormat.getImageableX(); double y = pageFormat.getImageableY(); double w = pageFormat.getImageableWidth(); double h = pageFormat.getImageableHeight(); this.chart.draw( g2, new Rectangle2D.Double(x, y, w, h), this.anchor, null ); */ return PAGE_EXISTS; }
/** * */ public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof mxGraphComponent) { mxGraphComponent graphComponent = (mxGraphComponent) e .getSource(); PrinterJob pj = PrinterJob.getPrinterJob(); if (pj.printDialog()) { PageFormat pf = graphComponent.getPageFormat(); Paper paper = new Paper(); double margin = 36; paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2); pf.setPaper(paper); pj.setPrintable(graphComponent, pf); try { pj.print(); } catch (PrinterException e2) { System.out.println(e2); } } } }
private static void printOneRowWithJavaPrintDialog() { final JTable table = createAuthorTable(1); Printable printable = table.getPrintable( JTable.PrintMode.NORMAL, new MessageFormat("Author Table"), new MessageFormat("Page - {0}")); PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(printable); boolean printAccepted = job.printDialog(); if (printAccepted) { try { job.print(); closeFrame(); } catch (PrinterException e) { throw new RuntimeException(e); } } }
/** * Prints the bill associated with the provided booking on a printer. * * @param booking The booking to print the bill for. * * @throws IOException * @throws PrinterException */ public void printBillForBooking(Booking booking) throws IOException, PrinterException { BillPdfGenerator pdfGenerator = new BillPdfGenerator(booking); try (PDDocument pdf = pdfGenerator.getBillAsPdf()) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintService(choosePrinter()); job.setPageable(new PDFPageable(pdf)); job.print(); } }
public int print(Graphics g, PageFormat pageFormat, int index) throws PrinterException { //out("PAPER IS: " + myPapers.size()); if (index == myPapers.size()) { return NO_SUCH_PAGE; } //out(" print: " + index); myPapers.get(index).print(g); return PAGE_EXISTS; }
@Override public void print(String name) { final MessageFormat header = new MessageFormat(name); final MessageFormat footer = new MessageFormat(vm().entityName() + ": " + codeViewerKindName() + " Printed: " + new Date() + " -- Page: {0, number, integer}"); try { table.print(JTable.PrintMode.FIT_WIDTH, header, footer); } catch (PrinterException printerException) { gui().errorMessage("Print failed: " + printerException.getMessage()); } }
public static void createUI() { f = new JFrame("LinearGradient Printing Test"); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); final LinearGradientPrintingTest gpt = new LinearGradientPrintingTest(); Container c = f.getContentPane(); c.add(BorderLayout.CENTER, gpt); final JButton print = new JButton("Print"); print.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(gpt); final boolean doPrint = job.printDialog(); if (doPrint) { try { job.print(); } catch (PrinterException ex) { throw new RuntimeException(ex); } } } }); c.add(print, BorderLayout.SOUTH); f.pack(); f.setVisible(true); }
/** * @return <code>true</code> on success. <code>false</code> if user aborts printing. */ public static boolean print(Printable printable) throws PrinterException { getPrinterJob().setPrintable(printable); if (getPrinterJob().printDialog()) { PrintingTools.getPrinterJob().print(printSettings); return true; } else { return false; } }
private static void printTexture() { f = new JFrame("Texture Printing Test"); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); final TexturePaintPrintingTest gpt = new TexturePaintPrintingTest(); Container c = f.getContentPane(); c.add(BorderLayout.CENTER, gpt); final JButton print = new JButton("Print"); print.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(gpt); final boolean doPrint = job.printDialog(); if (doPrint) { try { job.print(); } catch (PrinterException ex) { throw new RuntimeException(ex); } } } }); c.add(print, BorderLayout.SOUTH); f.pack(); f.setVisible(true); }
@Override public int print(java.awt.Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { // We have only one page, and 'page' is zero-based return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D) g; // User (0,0) is typically outside the imageable area, so we must // translate by the X and Y values in the PageFormat to avoid clipping g2d.translate(pf.getImageableX(), pf.getImageableY()); // Scale plots to paper size. double scaleX = pf.getImageableWidth() / contentPane.getWidth(); double scaleY = pf.getImageableHeight() / contentPane.getHeight(); g2d.scale(scaleX, scaleY); // Disable double buffering RepaintManager currentManager = RepaintManager.currentManager(this); currentManager.setDoubleBufferingEnabled(false); // Now we perform our rendering contentPane.printAll(g); // Enable double buffering currentManager.setDoubleBufferingEnabled(true); // tell the caller that this page is part of the printed document return PAGE_EXISTS; }
public void printableJob(Printable printable) throws PrintException { try { synchronized(this) { if (job != null) { // shouldn't happen throw new PrintException("already printing"); } else { job = new PSPrinterJob(); } } job.setPrintService(getPrintService()); job.setCopies(copies); job.setJobName(jobName); PageFormat pf = new PageFormat(); if (mediaSize != null) { Paper p = new Paper(); p.setSize(mediaSize.getX(MediaSize.INCH)*72.0, mediaSize.getY(MediaSize.INCH)*72.0); p.setImageableArea(72.0, 72.0, p.getWidth()-144.0, p.getHeight()-144.0); pf.setPaper(p); } if (orient == OrientationRequested.REVERSE_LANDSCAPE) { pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); } else if (orient == OrientationRequested.LANDSCAPE) { pf.setOrientation(PageFormat.LANDSCAPE); } job.setPrintable(printable, pf); 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 int print(Graphics g, PageFormat pf, int pi) throws PrinterException { if (pi != 0) { return NO_SUCH_PAGE; } Graphics2D g2 = (Graphics2D) g; g2.setFont(new Font("Serif", Font.PLAIN, 36)); g2.setPaint(Color.black); g2.drawString("Java Source and Support", 100, 100); Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf .getImageableY(), pf.getImageableWidth(), pf .getImageableHeight()); g2.draw(outline); return PAGE_EXISTS; }
@Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { ramusPrintable.setPageFormat(pageFormat); if (ramusPrintable.getPageCount() > pageIndex) { ramusPrintable.print((Graphics2D) graphics, pageIndex); return PAGE_EXISTS; } return NO_SUCH_PAGE; }
@Override public void actionPerformed(ActionEvent e) { try { printable.print(framework); setSize(); } catch (PrinterException e1) { if (framework != null) JOptionPane.showMessageDialog(framework.getMainFrame(), e1.getLocalizedMessage()); else JOptionPane.showMessageDialog(null, e1.getLocalizedMessage()); } }
public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); if (job.printDialog()) { try { job.print(); } catch (PrinterException ex) { ex.printStackTrace(); } } }
@Override public void print(GUIFramework framework) throws PrinterException { final PrinterJob pj = framework.getPrinterJob(getJobKey()); final Printable printable = createPrintable(); pj.setPrintable(printable, getPageFormat()); if (pj.printDialog()) { pj.setJobName(getJobName()); pj.print(); setPageFormat(getPageFormat()); } }
/** * The RastePrintJob super class calls this method * at the end of each page. */ protected void endPage(PageFormat format, Printable painter, int index) throws PrinterException { mPSStream.println(PAGE_RESTORE); mPSStream.println(SHOWPAGE); }
/** * 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 void validateDestination(String dest) throws PrinterException { if (dest == null) { return; } // dest is null for Destination(new URI("")) // because isAttributeValueSupported returns false in setAttributes // Destination(new URI(" ")) throws URISyntaxException File f = new File(dest); try { // check if this is a new file and if filename chars are valid if (f.createNewFile()) { f.delete(); } } catch (IOException ioe) { throw new PrinterException("Cannot write to file:"+ dest); } catch (SecurityException se) { //There is already file read/write access so at this point // only delete access is denied. Just ignore it because in // most cases the file created in createNewFile gets overwritten // anyway. } File pFile = f.getParentFile(); if ((f.exists() && (!f.isFile() || !f.canWrite())) || ((pFile != null) && (!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) { throw new PrinterException("Cannot write to file:"+ dest); } }
/** * Prints the page at the specified index into the specified * {@link Graphics} context in the specified * format. A {@code PrinterJob} calls the * {@code Printable} interface to request that a page be * rendered into the context specified by * {@code graphics}. The format of the page to be drawn is * specified by {@code pageFormat}. The zero based index * of the requested page is specified by {@code pageIndex}. * If the requested page does not exist then this method returns * NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned. * The {@code Graphics} class or subclass implements the * {@link java.awt.PrintGraphics} interface to provide additional * information. If the {@code Printable} object * aborts the print job then it throws a {@link PrinterException}. * @param graphics the context into which the page is drawn * @param pageFormat the size and orientation of the page being drawn * @param pageIndex the zero based index of the page to be drawn * @return PAGE_EXISTS if the page is rendered successfully * or NO_SUCH_PAGE if {@code pageIndex} specifies a * non-existent page. * @exception java.awt.print.PrinterException * thrown when the print job is terminated. */ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { int result; /* This method will be called by the PrinterJob on a thread other * that the application's thread. We hold on to the graphics * until we can rendevous with the application's thread and * hand over the graphics. The application then does all the * drawing. When the application is done drawing we rendevous * again with the PrinterJob thread and release the Graphics * so that it knows we are done. */ /* Add the graphics to the message queue of graphics to * be rendered. This is really a one slot queue. The * application's thread will come along and remove the * graphics from the queue when the app asks for a graphics. */ graphicsToBeDrawn.append( (Graphics2D) graphics); /* We now wait for the app's thread to finish drawing on * the Graphics. This thread will sleep until the application * release the graphics by placing it in the graphics drawn * message queue. If the application signals that it is * finished drawing the entire document then we'll get null * returned when we try and pop a finished graphic. */ if (graphicsDrawn.pop() != null) { result = PAGE_EXISTS; } else { result = NO_SUCH_PAGE; } return result; }
public void run() { try { printerJob.print(attributes); } catch (PrinterException e) { //REMIND: need to store this away and not rethrow it. } /* Close the message queues so that nobody is stuck * waiting for one. */ graphicsToBeDrawn.closeWhenEmpty(); graphicsDrawn.close(); }