/** * Prints using a custom page size and custom margins. */ private static void printWithPaper(PDDocument document) throws IOException, PrinterException { PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); // define custom paper Paper paper = new Paper(); paper.setSize(306, 396); // 1/72 inch paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); // no margins // custom page format PageFormat pageFormat = new PageFormat(); pageFormat.setPaper(paper); // override the page format Book book = new Book(); // append all pages book.append(new PDFPrintable(document), pageFormat, document.getNumberOfPages()); job.setPageable(book); job.print(); } }
public void printComponenet(){ PrinterJob pj = PrinterJob.getPrinterJob(); pj.setJobName(" Print Component "); pj.setPrintable (new Printable() { public int print(Graphics pg, PageFormat pf, int pageNum){ if (pageNum > 0){ return Printable.NO_SUCH_PAGE; } Graphics2D g2 = (Graphics2D) pg; g2.translate(pf.getImageableX(), pf.getImageableY()); componenet_name.paint(g2); return Printable.PAGE_EXISTS; } }); if (pj.printDialog() == false) return; try { pj.print(); } catch (PrinterException ex) { // handle exception } }
printServices = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet); pageFormat = new PageFormat(); // If you want to adjust heigh and width etc. of your paper. pageFormat = printerjob.defaultPage(); printerjob.setPrintable(new Server(), pageFormat); // Server was my class's name, you use yours. printerjob.setPrintService(printService); // Try setting the printer you want } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Error: No printer named '" + printerName + "', using default printer."); pageFormat = printerjob.defaultPage(); // Set the default printer instead. } catch (PrinterException exception) { System.err.println("Printing error: " + exception); printerjob.print(); // Actual print command } catch (PrinterException exception) { System.err.println("Printing error: " + exception);
public void actionPerformed(ActionEvent ae) { PRINTER_JOB.setPageable(textEditor); try { if (PRINTER_JOB.printDialog()) { PRINTER_JOB.print(); } } catch (Exception e) { e.printStackTrace(); } } } // end ConsoleTextEditor.PrintAction
/** * Prints with a print preview dialog. */ private static void printWithDialog(PDDocument document) throws IOException, PrinterException { PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); if (job.printDialog()) { job.print(); } }
PrinterJob pjob = PrinterJob.getPrinterJob(); PageFormat pf = pjob.defaultPage(); pf.setOrientation(PageFormat.LANDSCAPE); PageFormat postformat = pjob.pageDialog(pf); if (pf != postformat) { pjob.setPrintable(new ComponentPrinter(comp, fill), postformat); if (pjob.printDialog()) { pjob.print(); Paper paper = new Paper(); paper.setSize(8.3 * 72, 11.7 * 72); paper.setImageableArea(18, 18, 559, 783); PageFormat pf = new PageFormat(); pf.setPaper(paper); pf.setOrientation(PageFormat.LANDSCAPE); g2.translate(format.getImageableX(), format.getImageableY()); double width = (int) Math.floor(format.getImageableWidth());
if (pj.printDialog()) { PageFormat pf = pj.defaultPage(); Paper paper = pf.getPaper(); double width = fromCMToPPI(3.5); System.out.println("After- " + dump(pf)); dump(pf); PageFormat validatePage = pj.validatePage(pf); System.out.println("Valid- " + dump(validatePage)); pj.setPrintable(new MyPrintable(), pf); try { pj.print(); } catch (PrinterException ex) { ex.printStackTrace(); Graphics2D g2d = (Graphics2D) graphics; System.out.println("[Print] " + dump(pageFormat)); double width = pageFormat.getImageableWidth(); double height = pageFormat.getImageableHeight(); g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY()); g2d.draw(new Rectangle2D.Double(1, 1, width - 1, height - 1)); FontMetrics fm = g2d.getFontMetrics(); g2d.drawString("0x0", 0, fm.getAscent()); result = PAGE_EXISTS;
g2d.translate(pf.getImageableX(), pf.getImageableY()); g.drawString("Hello world!", 100, 100); job.setPrintable(this); job.setPrintService(printServices[0]); job.print(); } catch (PrinterException ex) { Logger.getLogger(HelloWorldPrinter.class.getName()).log(Level.SEVERE, null, ex);
private void drawString(Graphics g, String text, int x, int y) { for (String line : text.split("\n")) g.drawString(line, x, y += g.getFontMetrics().getHeight()); return NO_SUCH_PAGE; pageFormat.setOrientation(PageFormat.LANDSCAPE); Graphics2D g2d = (Graphics2D)graphics; g2d.setPaint(Color.black); g2d.setFont(new Font("Arial", Font.BOLD, 10)); g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(contentToPrint); try { job.printDialog(); job.print(); } catch (PrinterException e) { System.err.println(e.getMessage());
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(MediaSizeName.ISO_A4); aset.add(pr); aset.add(OrientationRequested.PORTRAIT); pj.setPrintable(new Page()); try { pj.print(aset); } catch (PrinterException ex) { ex.printStackTrace(); g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); g.setFont(new Font("Arial", Font.PLAIN, 128)); FontMetrics fm = g.getFontMetrics(); int x = (int)(pageFormat.getWidth() - fm.stringWidth("A")) / 2; int y = (int)((pageFormat.getHeight() - fm.getHeight()) / 2) + fm.getAscent(); g2d.drawString("A", x, y);
PageFormat format = new PageFormat(); Paper paper = new Paper(); double bottomMargin = 0.01; format.setPaper(paper); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(OrientationRequested.PORTRAIT); PrinterJob printerJob = PrinterJob.getPrinterJob(); Printable printable = new ReceiptPrint(); format = printerJob.validatePage(format); boolean don = printerJob.printDialog(); printerJob.setPrintable(printable, format); try { printerJob.print(aset); } catch (Exception e) { e.printStackTrace(); textarea.setEditable(false); g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); g2d.drawString(strText, 1, (lineNumber + 1) * 18);
PrinterJob pj = PrinterJob.getPrinterJob(); pj.setJobName("Book 'em Danno"); PageFormat pf = pj.defaultPage(); pf.setOrientation(PageFormat.LANDSCAPE); Book book = new Book(); for (int index = 0; index < 10; index++) { book.append(new Page(index + 1), pf); pj.setPageable(book); if (pj.printDialog()) { try { pj.print(); } catch (PrinterException ex) { ex.printStackTrace(); g2.translate(pf.getImageableX(), pf.getImageableY()); g2.translate(0f, 0f); FontMetrics fm = g2.getFontMetrics(); String text = Integer.toString(page); double x = (pf.getImageableWidth() - fm.stringWidth(text)) / 2d; g2.drawString(text, (float)x, (float)y);
printjob.setJobName(" TESSCO CUSTOMER CARD "); g2.translate(pf.getImageableX(), pf.getImageableY()); g2.drawImage(bufferedImage, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null); Paper paper = new Paper(); paper.setImageableArea(0, 0, 153, 243); paper.setSize(243, 154); PageFormat format = new PageFormat(); format.setOrientation(PageFormat.LANDSCAPE); printjob.setPrintable(printable, format); if (printjob.printDialog() == false) return; printjob.print(); } catch (PrinterException ex) { System.out.println("NO PAGE FOUND." + ex);
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); g2d.setFont(new Font("Monospaced", Font.BOLD, 7)); for (int i = 0; i < Bill.length; i++) { g2d.drawString(Bill[i], 0, y); y = y + 15; PageFormat pageFormat = new PageFormat(); pageFormat.setOrientation(PageFormat.PORTRAIT); Paper pPaper = pageFormat.getPaper(); pPaper.setImageableArea(0, 0, pPaper.getWidth() , pPaper.getHeight() -2); pageFormat.setPaper(pPaper); job.setPrintable(contentToPrint, pageFormat); job.print();
job.setPrintable(this); boolean doPrint = job.printDialog(); job.print(); }catch(PrinterException e){ System.out.println(e.getStackTrace()); g2d.translate(pf.getImageableX(), pf.getImageableY()); g.drawString(i, 100, spaceCounter); spaceCounter+=50;
g2.setFont(new Font("Serif", Font.PLAIN, 12)); g2.setPaint(Color.black); myDrawString(g2, builder.toString(), 100, 100); g.drawString(line, x, y += g.getFontMetrics().getHeight()); PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintable(new PrintableDemo1()); pj.print(); } catch (PrinterException e) { e.printStackTrace();
HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet(); attr.add(new MediaPrintableArea(0f, 0f, incomingImage.getWidth() / 72f, incomingImage.getHeight() / 72f, MediaPrintableArea.INCH)); job.setPrintable(this); PageFormat pf = job.pageDialog(job.defaultPage()); boolean ok = job.printDialog(); job.print(attr); } catch (PrinterException ex) { System.out.println("The job did not successfully complete"); Graphics2D g2d = (Graphics2D) g; g2d.translate(pf.getImageableX() - 1.0, pf.getImageableY() - 1.0); g.drawImage(incomingImage, 0, 0, 595, pageHeight, 0, page*pageHeight, 595, pageHeight*(page+1), null);
public void print(String imageFileName) throws IOException, PrinterException { final PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); final BufferedImage image = ImageIO.read(new File(imageFileName)); final PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setJobName("MyApp: " + imageFileName); printJob.setPrintService(printService); printJob.setPrintable(new Printable() { @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex == 0) { final Paper paper = pageFormat.getPaper(); paper.setImageableArea(0.0, 0.0, pageFormat.getPaper().getWidth(), pageFormat.getPaper().getHeight()); pageFormat.setPaper(paper); graphics.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY()); graphics.drawImage(image, 0, 0, (int) pageFormat.getPaper().getWidth(), (int) pageFormat.getPaper().getHeight(), null); return PAGE_EXISTS; } else { return NO_SUCH_PAGE; } } }); printJob.print(); }
PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(new ImagePrintable(printJob, image)); if (printJob.printDialog()) { try { printJob.print(); } catch (PrinterException prt) { prt.printStackTrace(); PageFormat pageFormat = printJob.defaultPage(); this.x = pageFormat.getImageableX(); this.y = pageFormat.getImageableY(); this.width = pageFormat.getImageableWidth(); this.orientation = pageFormat.getOrientation(); this.image = image; pWidth = pHeight * image.getWidth() / image.getHeight(); g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null); return PAGE_EXISTS; } else {
public static void printCard(final String name, final String lName, final Image mainImage){ Printable contentToPrint = new Printable(){ @Override public int print(Graphics graphics, PageFormat pageFormat, int page) throws PrinterException { if (page > 0) { return NO_SUCH_PAGE; } pageFormat.setOrientation(PageFormat.PORTRAIT); graphics.drawImage(mainImage, 0, 0, null); graphics.drawString(lName, 100, 300); graphics.drawString(name, 100, 100); return PAGE_EXISTS; } }; PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(contentToPrint); //You can show a print dialog before printing by job by wrapping the following blocks with a conditional statement if(job.printDialog()){...} try { job.print(); } catch (PrinterException e) { System.err.println(e.getMessage()); } }