/** * Get an instance of {@link java.awt.print.PageFormat}. * @param pj {@link java.awt.print.PrinterJob} which is * associated with the default printer. * @return an instance of <code>PageFormat</code> that describes the size and * orientation of a page to be printed. */ public static PageFormat getPageFormat(PrinterJob pj) { PageFormat pageFormat = null; pageFormat = pj.defaultPage(); Paper p = pageFormat.getPaper(); int pageOrientation = getPreferences().getInt(PROP_PAGE_ORIENTATION, pageFormat.getOrientation()); double paperWidth = getPreferences().getDouble(PROP_PAGE_WIDTH, p.getWidth()); double paperHeight = getPreferences().getDouble(PROP_PAGE_HEIGHT, p.getHeight()); double iaWidth = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_WIDTH, p.getImageableWidth()); double iaHeight = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_HEIGHT, p.getImageableHeight()); double iaX = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_X, p.getImageableX()); double iaY = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_Y, p.getImageableY()); pageFormat.setOrientation(pageOrientation); p.setSize(paperWidth, paperHeight); p.setImageableArea(iaX, iaY, iaWidth, iaHeight); pageFormat.setPaper(p); return pageFormat; }
/** * */ 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); } } } }
public PageFormat getPageFormat() { PrinterJob job = PrinterJob.getPrinterJob(); if (myPageFormat == null) { myPageFormat = job.defaultPage(); // restore myPageFormat.setOrientation(round(get(PAGE_ORIENTATION, PageFormat.PORTRAIT))); Paper paper = myPageFormat.getPaper(); if (get(PAPER_WIDTH, null) != null && get(PAPER_HEIGHT, null) != null) { paper.setSize(get(PAPER_WIDTH, INCH), get(PAPER_HEIGHT, INCH)); } if (get(AREA_X, null) != null && get(AREA_Y, null) != null && get(AREA_WIDTH, null) != null && get(AREA_HEIGHT, null) != null) { paper.setImageableArea(get(AREA_X, INCH), get(AREA_Y, INCH), get(AREA_WIDTH, INCH), get(AREA_HEIGHT, INCH)); } myPageFormat.setPaper(paper); } return myPageFormat; }
private static void printTest() { PrinterJob pj = PrinterJob.getPrinterJob(); PageFormat pf = pj.defaultPage(); Paper paper = new Paper(); double margin = 36; // half inch paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2); pf.setPaper(paper); pj.setPrintable(new PrintTestLexmarkIQ(), pf); if (pj.printDialog()) { try { pj.print(); } catch (PrinterException e) { System.out.println(e); } } }
public void testGetPageFormat() { PrinterJob pj = PrinterJob.getPrinterJob(); PageFormat expResult = PrintPreferences.getPageFormat(pj); PrintPreferences.setPageFormat(expResult); PageFormat result = PrintPreferences.getPageFormat(pj); assertEquals(expResult.getHeight(), result.getHeight()); assertEquals(expResult.getWidth(), result.getWidth()); assertEquals(expResult.getOrientation(), result.getOrientation()); assertEquals(expResult.getPaper().getHeight(), result.getPaper().getHeight()); assertEquals(expResult.getPaper().getWidth(), result.getPaper().getWidth()); assertEquals(expResult.getPaper().getImageableHeight(), result.getPaper().getImageableHeight()); assertEquals(expResult.getPaper().getImageableWidth(), result.getPaper().getImageableWidth()); assertEquals(expResult.getPaper().getImageableX(), result.getPaper().getImageableX()); assertEquals(expResult.getPaper().getImageableY(), result.getPaper().getImageableY()); double w = expResult.getPaper().getWidth() + 10; double h = expResult.getPaper().getHeight() + 10; Paper p = expResult.getPaper(); double ix = p.getImageableX() + 10; double iy = p.getImageableY() + 10; double iw = p.getImageableWidth() + 10; double ih = p.getImageableHeight() + 10; p.setImageableArea(ix, iy, iw, ih); p.setSize(w, h); expResult.setPaper(p); PrintPreferences.setPageFormat(expResult); assertEquals(h, PrintPreferences.getPageFormat(pj).getHeight()); assertEquals(w, PrintPreferences.getPageFormat(pj).getWidth()); assertEquals(ix, PrintPreferences.getPageFormat(pj).getPaper().getImageableX()); assertEquals(iy, PrintPreferences.getPageFormat(pj).getPaper().getImageableY()); assertEquals(iw, PrintPreferences.getPageFormat(pj).getPaper().getImageableWidth()); assertEquals(ih, PrintPreferences.getPageFormat(pj).getPaper().getImageableHeight()); expResult.setOrientation(PageFormat.REVERSE_LANDSCAPE); PrintPreferences.setPageFormat(expResult); assertEquals(PageFormat.REVERSE_LANDSCAPE, PrintPreferences.getPageFormat(pj).getOrientation()); }
public boolean showPageSetup() { PrinterJob job = PrinterJob.getPrinterJob(); PageFormat oldFormat = getPageFormat(); PageFormat newFormat = job.pageDialog(oldFormat); if (oldFormat == newFormat) { return false; } myPageFormat = newFormat; // save set(PAGE_ORIENTATION, myPageFormat.getOrientation()); Paper paper = myPageFormat.getPaper(); set(PAPER_WIDTH, paper.getWidth()); set(PAPER_HEIGHT, paper.getHeight()); set(AREA_X, paper.getImageableX()); set(AREA_Y, paper.getImageableY()); set(AREA_WIDTH, paper.getImageableWidth()); set(AREA_HEIGHT, paper.getImageableHeight()); return true; }
public EPSPrinter(Printable printable, String title, PrintStream stream, int x, int y, int wid, int hgt) { this.printable = printable; this.epsTitle = title; this.stream = stream; llx = x; lly = y; urx = llx+wid; ury = lly+hgt; // construct a PageFormat with zero margins representing the // exact bounds of the applet. ie construct a theoretical // paper which happens to exactly match applet panel size. Paper p = new Paper(); p.setSize((double)wid, (double)hgt); p.setImageableArea(0.0,0.0, (double)wid, (double)hgt); pf = new PageFormat(); pf.setPaper(p); }
/** * */ 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 doTest() { PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(Chromaticity.MONOCHROME); MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5); float[] size = isoA5Size.getSize(Size2DSyntax.INCH); Paper paper = new Paper(); paper.setSize(size[0] * 72.0, size[1] * 72.0); paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0); PageFormat pf = new PageFormat(); pf.setPaper(paper); PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf)); if (job.printDialog()) { try { job.print(aset); } catch (PrinterException pe) { throw new RuntimeException(pe); } } }
public static void main(String args[]) { PageFormat pf; pf = (PageFormat) new PageFormat().clone(); pf.getWidth(); pf.getHeight(); pf.getImageableX(); pf.getImageableY(); pf.getImageableWidth(); pf.getImageableHeight(); pf.getPaper(); pf.setPaper(new Paper()); pf.setOrientation(PageFormat.PORTRAIT); if (pf.getOrientation() != PageFormat.PORTRAIT) throw new RuntimeException("Changing Orientation did not result in a change: PageFormat.PORTRAIT"); pf.setOrientation(PageFormat.LANDSCAPE); if (pf.getOrientation() != PageFormat.LANDSCAPE) throw new RuntimeException("Changing Orientation did not result in a change: PageFormat.LANDSCAPE"); pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); if (pf.getOrientation() != PageFormat.REVERSE_LANDSCAPE) throw new RuntimeException("Changing Orientation did not result in a change: PageFormat.REVERSE_LANDSCAPE"); pf.getOrientation(); pf.getMatrix(); }
/** * Sets the width for this page. This also validates the imageable bounds of * the paper. */ public boolean setPaperWidth(double width) { if (width == pageFormat.getWidth()) return false; double oldWidth = pageFormat.getWidth(); Paper paper = pageFormat.getPaper(); paper.setSize(width, paper.getHeight()); pageFormat.setPaper(paper); firePropertyChange(PROPERTY_WIDTH, new Double(oldWidth), new Double( width)); validateImageableBounds(); return true; }
/** * Sets the height for this page. This also validates the imageable bounds * of the paper. */ public boolean setPaperHeight(double height) { if (height == pageFormat.getHeight()) return false; double oldHeight = pageFormat.getHeight(); Paper paper = pageFormat.getPaper(); paper.setSize(paper.getWidth(), height); pageFormat.setPaper(paper); firePropertyChange(PROPERTY_WIDTH, new Double(oldHeight), new Double( height)); validateImageableBounds(); return true; }
private PageFormat getPageFormat() { final double mmToDots = 72 / 2.54; SettingsModel settings = Controller.get().getConfig(); Paper p = new Paper(); p.setSize( settings.getValue("PageWidht", 21.0 * mmToDots), settings.getValue("PageHeight", 29.7 * mmToDots) ); p.setImageableArea( settings.getValue("PageImageableX", 2.5 * mmToDots), settings.getValue("PageImageableY", 1.0 * mmToDots), settings.getValue("PageImageableWidth", 17.5 * mmToDots), settings.getValue("PageImageableHeight", 27.7 * mmToDots) ); PageFormat pf = new PageFormat(); pf.setPaper(p); pf.setOrientation(settings.getValue("PageOrientation", PageFormat.PORTRAIT)); return pf; }
public void actionPerformed(ActionEvent e) { PrinterJob pj = PrinterJob.getPrinterJob(); if (pj.printDialog()) { PageFormat pf = frame.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(textArea, pf); try { // Don't delete the float cast! If done, deriveFont will change the style instead of the font size. textArea.setFont(textArea.getFont().deriveFont((float)(textArea.getFont().getSize() - 5))); pj.print(); } catch (PrinterException e2) { JOptionPane.showMessageDialog(frame, "Error de impresión", "Error", JOptionPane.ERROR_MESSAGE); } textArea.setFont(textArea.getFont().deriveFont((float)(textArea.getFont().getSize() + 5))); } }
/** * Gets a page format instance that describes this page's geometry in terms * of the Java print API. * * @return A throwaway PageFormat object that describes this page's current * geometry. Changes to the returned PageFormat object will not * affect this page in any way. */ public PageFormat getPageFormat() { PageFormat pageFormat = new PageFormat(); pageFormat.setOrientation(getOrientation().getPrintApiCode()); Paper paper = new Paper(); if (getOrientation() == PageOrientation.PORTRAIT) { paper.setSize(getWidth(), getHeight()); } else { paper.setSize(getHeight(), getWidth()); } // the imageable area on the page format we return determines the clipping // region for the print API, so we always want to set it as big as possible // regardless of our own margin guides paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); pageFormat.setPaper(paper); return pageFormat; }
/** * Applies the given Java print API page format to this page object. The aspects that * are affected are: * <ul> * <li>width (paper width) * <li>height (paper height) * <li>orientation * <li>TODO: margins (determined by imageable area) * </ul> * If any changes are made to the page as a result of this operation, property change * events for the individually affected properties will be fired as if those methods * had been called directly. * * @param pageFormat The page format to apply to this page. */ public void applyPageFormat(PageFormat pageFormat) { Paper paper = pageFormat.getPaper(); setOrientation(PageOrientation.forPrintApiCode(pageFormat.getOrientation())); if (WabitUtils.getWorkspace(this).isMagicEnabled()) { // important to set the page dimensions after orientation so // setOrientation() doesn't swap them inappropriately if (getOrientation() == PageOrientation.PORTRAIT) { setWidth((int) paper.getWidth()); setHeight((int) paper.getHeight()); } else { setWidth((int) paper.getHeight()); setHeight((int) paper.getWidth()); } } // TODO update margins from page format? }
/** * Returns the page format for printing. */ public PageFormat pageFormat() { if (fPageFormat == null) { // initialize with defaults PrinterJob job = PrinterJob.getPrinterJob(); fPageFormat = job.defaultPage(); Paper p = fPageFormat.getPaper(); p.setSize(Options.PRINT_PAGEFORMAT_WIDTH, Options.PRINT_PAGEFORMAT_HEIGHT); fPageFormat.setPaper(p); if (Options.PRINT_PAGEFORMAT_ORIENTATION.equals("portrait")) fPageFormat.setOrientation(PageFormat.PORTRAIT); else if (Options.PRINT_PAGEFORMAT_ORIENTATION.equals("landscape")) fPageFormat.setOrientation(PageFormat.LANDSCAPE); else if (Options.PRINT_PAGEFORMAT_ORIENTATION.equals("seascape")) fPageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE); } return fPageFormat; }
public static void main(String[] args) throws Exception { PDFJob job=new PDFJob(new FileOutputStream("test.pdf")); PageFormat pf=new PageFormat(); pf.setOrientation(PageFormat.PORTRAIT); Paper p = new Paper(); p.setSize(210,297); //A4 pf.setPaper(p); BufferedImage img = ImageIO.read(new File("earth.jpg")); int w = 200; for(int i=0;i<10;i++){ Graphics g = job.getGraphics(); g.drawImage(img, 0, 0,w,w, null); g.dispose(); } job.end(); }
/** * @param pPageFormat * @param pPageFormatProperty */ public static void setPageFormatFromString(Paper pPaper, String pPageFormatProperty) { try { // parse string: StringTokenizer tokenizer = new StringTokenizer( pPageFormatProperty, ";"); if (tokenizer.countTokens() != 6) { logger.warning("Page format property has not the correct format:" + pPageFormatProperty); return; } pPaper.setSize(nt(tokenizer), nt(tokenizer)); pPaper.setImageableArea(nt(tokenizer), nt(tokenizer), nt(tokenizer), nt(tokenizer)); } catch (Exception e) { freemind.main.Resources.getInstance().logException(e); } }
/** Writes a PageFormat instance into ObjectOutput * @param pf * @param obtos */ private static void externalizePageFormat(PageFormat pf, ObjectOutput obtos) throws IOException { if (pf == null) { obtos.writeInt(PageFormat.LANDSCAPE ^ PageFormat.REVERSE_LANDSCAPE ^ PageFormat.PORTRAIT); return; } obtos.writeInt(pf.getOrientation()); Paper paper = pf.getPaper(); // paper size obtos.writeDouble(paper.getWidth()); obtos.writeDouble(paper.getHeight()); // paper imageable area obtos.writeDouble(paper.getImageableX()); obtos.writeDouble(paper.getImageableY()); obtos.writeDouble(paper.getImageableWidth()); obtos.writeDouble(paper.getImageableHeight()); }
/** Reads a PageFormat instance from ObjectInput * @param obtis * @return deserialized PageFormat instance */ private static PageFormat internalizePageFormat(ObjectInput obtis) throws IOException, ClassNotFoundException { PageFormat pf = new PageFormat(); Paper paper = pf.getPaper(); int etc = obtis.readInt(); if (etc == (PageFormat.LANDSCAPE ^ PageFormat.REVERSE_LANDSCAPE ^ PageFormat.PORTRAIT)) { return null; } pf.setOrientation(etc); // paper size paper.setSize(obtis.readDouble(), obtis.readDouble()); // imageable paper.setImageableArea(obtis.readDouble(), obtis.readDouble(), obtis.readDouble(), obtis.readDouble()); pf.setPaper(paper); return pf; }
/** @return A newly created {@link PageFormat} with the current settings. */ public PageFormat createPageFormat() { PageFormat format = new PageFormat(); PageOrientation orientation = getPageOrientation(); double[] size = getPaperSize(LengthUnits.PT); double[] margins = getPaperMargins(LengthUnits.PT); Paper paper = new Paper(); if (orientation == PageOrientation.PORTRAIT) { format.setOrientation(PageFormat.PORTRAIT); } else if (orientation == PageOrientation.LANDSCAPE) { format.setOrientation(PageFormat.LANDSCAPE); } else if (orientation == PageOrientation.REVERSE_PORTRAIT) { // REVERSE_PORTRAIT doesn't exist in the old PageFormat class format.setOrientation(PageFormat.PORTRAIT); } else if (orientation == PageOrientation.REVERSE_LANDSCAPE) { format.setOrientation(PageFormat.REVERSE_LANDSCAPE); } paper.setSize(size[0], size[1]); paper.setImageableArea(margins[1], margins[0], size[0] - (margins[1] + margins[3]), size[1] - (margins[0] + margins[2])); format.setPaper(paper); return format; }
protected PdfTemplate transSVG( String svgPath, byte[] svgData, float x, float y, float height, float width, String helpText ) throws IOException, DocumentException { PdfTemplate template = contentByte.createTemplate( width, height ); Graphics2D g2D = template.createGraphics( width, height ); PrintTranscoder transcoder = new PrintTranscoder( ); if ( null != svgData && svgData.length > 0 ) { transcoder.transcode( new TranscoderInput( new ByteArrayInputStream( svgData ) ), null ); } else if ( null != svgPath ) { transcoder.transcode( new TranscoderInput( svgPath ), null ); } PageFormat pg = new PageFormat( ); Paper p = new Paper( ); p.setSize( width, height ); p.setImageableArea( 0, 0, width, height ); pg.setPaper( p ); transcoder.print( g2D, pg, 0 ); g2D.dispose( ); return template; }
public static void setPageFormat(String name, PageFormat pageFormat) { Properties properties = getProperties(name); switch (pageFormat.getOrientation()) { case PageFormat.LANDSCAPE: properties.setProperty(ORIENTATION, LANDSCAPE); break; case PageFormat.PORTRAIT: properties.setProperty(ORIENTATION, PORTRAIT); break; case PageFormat.REVERSE_LANDSCAPE: properties.setProperty(ORIENTATION, REVERSE_LANDSCAPE); break; default: properties.setProperty(ORIENTATION, "EMPTY"); } ; final Paper paper = pageFormat.getPaper(); properties.setProperty(PAPER_IMAGEABLE_HEIGHT, Double.toString(paper .getImageableHeight())); properties.setProperty(PAPER_IMAGEABLE_WIDTH, Double.toString(paper .getImageableWidth())); properties.setProperty(PAPER_IMAGEABLE_X, Double.toString(paper .getImageableX())); properties.setProperty(PAPER_IMAGEABLE_Y, Double.toString(paper .getImageableY())); properties .setProperty(PAPER_HEIGHT, Double.toString(paper.getHeight())); properties.setProperty(PAPER_WIDTH, Double.toString(paper.getWidth())); }
/** * The passed in PageFormat is cloned and altered to be usable on * the PrinterJob's current printer. */ public PageFormat validatePage(PageFormat page) { PageFormat newPage = (PageFormat)page.clone(); Paper newPaper = new Paper(); validatePaper(newPage.getPaper(), newPaper); newPage.setPaper(newPaper); return newPage; }
/** * The passed in PageFormat is cloned and altered to be usable on * the PrinterJob's current printer. */ private PageFormat validatePage(PageFormat page) { PageFormat newPage = (PageFormat)page.clone(); Paper newPaper = new Paper(); validatePaper(newPage.getPaper(), newPaper); newPage.setPaper(newPaper); return newPage; }