/** * 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(); } }
/** * Invoked when an action occurs. */ public void actionPerformed( final ActionEvent e ) { final String selectedItem = (String) pageFormatBox.getSelectedItem(); if ( selectedItem == null ) { return; } final Paper paper = PageFormatFactory.getInstance().createPaper( selectedItem ); if ( paper == null ) { return; } pageWidthField.setText( String.valueOf( paper.getWidth() ) ); pageHeightField.setText( String.valueOf( paper.getHeight() ) ); } }
private static String printPaper( final Paper paper ) { StringBuffer b = new StringBuffer(); b.append( "Paper={width=" ); b.append( paper.getWidth() ); b.append( ", height=" ); b.append( paper.getHeight() ); b.append( ", imageableX=" ); b.append( paper.getImageableX() ); b.append( ", imageableY=" ); b.append( paper.getImageableY() ); b.append( ", imageableWidth=" ); b.append( paper.getImageableWidth() ); b.append( ", imageableHeight=" ); b.append( paper.getImageableHeight() ); b.append( "}" ); return b.toString(); }
/** * Returns the right border of the given paper. * * @param p * the paper that defines the borders. * @return the right border. */ public double getRightBorder( final Paper p ) { return p.getWidth() - ( p.getImageableX() + p.getImageableWidth() ); }
public void pageDialog() { this.pageFormat = getPrinterJob().pageDialog(getPageFormat()); Paper paper = pageFormat.getPaper(); StringBuilder sb = new StringBuilder(); NumberFormat format = DecimalFormat.getInstance(Locale.US); double paperWidth = paper.getWidth(); sb.append(format.format(paperWidth)).append(' '); double paperHeight = paper.getHeight(); sb.append(format.format(paperHeight)).append(' '); double imageableX = paper.getImageableX(); sb.append(format.format(imageableX)).append(' '); double imageableY = paper.getImageableY(); sb.append(format.format(imageableY)).append(' '); double imageableWidth = paper.getImageableWidth(); sb.append(format.format(imageableWidth)).append(' '); double imageableHeight = paper.getImageableHeight(); sb.append(format.format(imageableHeight)).append(' '); int pageFormatOrientation = pageFormat.getOrientation(); sb.append(format.format(pageFormatOrientation)).append(' '); ResourceController.getResourceController().setProperty("pageSettings", sb.toString()); }
public int print(Graphics g, PageFormat pageFormat, int pageIndex) { System.err.println(pageFormat.getPaper().getWidth()); if (pageIndex > 0) { return(NO_SUCH_PAGE); } else { Graphics2D g2d = (Graphics2D)g; g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); double dl = pageFormat.getImageableWidth(); double dh = pageFormat.getImageableHeight(); double scale = Math.min(dl / componentToBePrinted.getWidth(), dh / componentToBePrinted.getHeight()); g2d.scale(scale, scale); disableDoubleBuffering(componentToBePrinted); componentToBePrinted.paint(g2d); enableDoubleBuffering(componentToBePrinted); return(PAGE_EXISTS); } }
/** Patch for a bug in the PageFormat class * @param pf PageFormat * @return imageable y coordination for this page format */ private float getImageableYPatch(PageFormat pf) { if (pf.getOrientation() == PageFormat.LANDSCAPE) { double ret = pf.getPaper().getWidth() - (pf.getImageableY() + pf.getImageableHeight()); return (float) Math.round(ret); } else { return (float) pf.getImageableY(); } }
/** Patch for a bug in the PageFormat class * @param pf PageFormat * @return imageable y coordination for this page format */ private float getImageableYPatch(PageFormat pf) { if (pf.getOrientation() == PageFormat.LANDSCAPE) { double ret = pf.getPaper().getWidth() - (pf.getImageableY() + pf.getImageableHeight()); return (float) Math.round(ret); } else { return (float) pf.getImageableY(); } }
/** Patch for a bug in the PageFormat class * @param pf PageFormat * @return imageable y coordination for this page format */ private float getImageableYPatch(PageFormat pf) { if (pf.getOrientation() == PageFormat.LANDSCAPE) { double ret = pf.getPaper().getWidth() - (pf.getImageableY() + pf.getImageableHeight()); return (float) Math.round(ret); } else { return (float) pf.getImageableY(); } }
final Paper paper = new Paper(); paper.setSize(pageWidth * 72.0f, pageHeight * 72.0f); paper.setImageableArea(0.0, 0.0, paper.getWidth(), paper.getHeight()); pf.setPaper(paper);
/** * Returns the borders for the given paper. * * @param p * the paper. * @return The borders. */ private static Insets getBorders( final Paper p ) { return new Insets( (int) p.getImageableY(), (int) p.getImageableX(), (int) ( p.getHeight() - ( p.getImageableY() + p.getImageableHeight() ) ), (int) ( p.getWidth() - ( p .getImageableX() + p.getImageableWidth() ) ) ); }
/** * Resolves a page format, so that the result can be serialized. * * @param format the page format that should be prepared for serialisation. * @return the prepared page format data. */ private Object[] resolvePageFormat( final PageFormat format ) { final Integer orientation = new Integer( format.getOrientation() ); final Paper p = format.getPaper(); final float[] fdim = new float[] { (float) p.getWidth(), (float) p.getHeight() }; final float[] rect = new float[] { (float) p.getImageableX(), (float) p.getImageableY(), (float) p.getImageableWidth(), (float) p.getImageableHeight() }; return new Object[] { orientation, fdim, rect }; }
public DefaultPrinter(WaveView ecgview, PageFormat pageFormat) { this.ecgview = ecgview; Paper paper = pageFormat.getPaper(); double margin = 18; paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2); pageFormat.setPaper(paper); setLayout(null); removeAll(); int pageWidth = (int) pageFormat.getWidth(); int pageHeight = (int) pageFormat.getHeight(); this.setPreferredSize(new Dimension(pageWidth, pageHeight)); }
PDFFile pdfFile = new PDFFile(ByteBuffer.wrap(pdfBytes)); PDFPrintPage pages = new PDFPrintPage(pdfFile); PrinterJob job = PrinterJob.getPrinterJob(); Paper paper = new Paper(); paper.setSize(595, 842); // A4 dimensions in font points // Make the document fill the whole page without stupid extra margins! paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); PageFormat pageFormat = new PageFormat(); // or printerJob.defaultPage(); pageFormat.setPaper(paper); pageFormat.setOrientation(PageFormat.PORTRAIT); Book book = new Book(); book.append(pages, pageFormat, pdfFile.getNumPages()); job.setPageable(book); if (job.printDialog()) { job.print(); }
PDDocument doc = PDDocument.load(new File("test.pdf")); PrinterJob job = PrinterJob.getPrinterJob(); // 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(doc, Scaling.SHRINK_TO_FIT), pageFormat, doc.getNumberOfPages()); job.setPageable(book); job.print();
/** * Returns the borders for the given paper. * * @param p * the paper. * @return The borders. */ private int[] getBorders( final Paper p ) { final int[] retval = new int[4]; retval[ReportConfigWriter.TOP_BORDER] = (int) p.getImageableY(); retval[ReportConfigWriter.LEFT_BORDER] = (int) p.getImageableX(); retval[ReportConfigWriter.BOTTOM_BORDER] = (int) ( p.getHeight() - ( p.getImageableY() + p.getImageableHeight() ) ); retval[ReportConfigWriter.RIGHT_BORDER] = (int) ( p.getWidth() - ( p.getImageableX() + p.getImageableWidth() ) ); return retval; } }
PrinterJob job = PrinterJob.getPrinterJob(); PageFormat pf = job.defaultPage(); Paper paper = pf.getPaper(); double margin = 20.; paper.setImageableArea(margin, paper.getImageableY(), paper.getWidth() - 2* margin, paper.getImageableHeight()); pf.setPaper(paper); job.setPrintable(table.getPrintable(JTable.PrintMode.FIT_WIDTH, null, null), job.validatePage(pf)); job.print();
/** * Conversion d'un format en 1/72ieme d'inches en mm. * @param _pf Le format de page en 1/72ieme d'inches. * @return Un nouveau format de page en mm. */ public static PageFormat convert2MM(PageFormat _pf) { PageFormat r= (PageFormat)_pf.clone(); Paper pp= _pf.getPaper(); Paper ppmm= new Paper(); ppmm.setSize(pp.getWidth() * ImpToMM, pp.getHeight() * ImpToMM); ppmm.setImageableArea( pp.getImageableX() * ImpToMM, pp.getImageableY() * ImpToMM, pp.getImageableWidth() * ImpToMM, pp.getImageableHeight() * ImpToMM); r.setPaper(ppmm); return r; } /**
/** * @param pf <code>PageFormat</code> that describes the size and * orientation of a page to be printed */ public static void setPageFormat(PageFormat pf) { getPreferences().putInt(PROP_PAGE_ORIENTATION, pf.getOrientation()); getPreferences().putDouble(PROP_PAGE_WIDTH, pf.getPaper().getWidth()); getPreferences().putDouble(PROP_PAGE_HEIGHT, pf.getPaper().getHeight()); getPreferences().putDouble(PROP_PAGE_IMAGEABLEAREA_WIDTH, pf.getPaper().getImageableWidth()); getPreferences().putDouble(PROP_PAGE_IMAGEABLEAREA_HEIGHT, pf.getPaper().getImageableHeight()); getPreferences().putDouble(PROP_PAGE_IMAGEABLEAREA_X, pf.getPaper().getImageableX()); getPreferences().putDouble(PROP_PAGE_IMAGEABLEAREA_Y, pf.getPaper().getImageableY()); }
public static void pageSetup(View view) { job = getPrintJob(); PageFormat newFormat = job.pageDialog(format); if(newFormat != null) { format = newFormat; jEdit.setIntegerProperty("print.orientation",format.getOrientation()); Paper paper=format.getPaper(); jEdit.setDoubleProperty("print.width",paper.getImageableWidth()); jEdit.setDoubleProperty("print.height",paper.getImageableHeight()); jEdit.setDoubleProperty("print.x",paper.getImageableX()); jEdit.setDoubleProperty("print.y",paper.getImageableY()); jEdit.setDoubleProperty("print.pagewidth",paper.getWidth()); jEdit.setDoubleProperty("print.pageheight",paper.getHeight()); } } //}}}