Graphics2DRenderer gr = new Graphics2DRenderer(); gr.setDocument(doc, uri); gr.layout(g2, dim); Rectangle minSize = r.getMinimumSize(); //create new image with this size gr.render(newG2);
/** * A static utility method to automatically create an image from a * document, where height is determined based on document content. * To estimate a size before rendering, use {@link #setDocument(String)} * and then {@link #getMinimumSize()}. The rendered image supports transparency. * * @param url java.net.URL for the document to render. * @param width Width in pixels of the layout container * @return Returns an java.awt.Image containing the rendered document. */ public static BufferedImage renderToImageAutoSize(String url, int width){ return renderToImageAutoSize(url, width, BufferedImage.TYPE_INT_ARGB); }
/** * A static utility method to automatically create an image from a * document; the image supports transparency. To render an image that does not support transparency, * use the overloaded version of this method {@link #renderToImage(String, int, int, int)}. * * @param url URL for the document to render. * @param width Width in pixels of the layout container * @param height Height in pixels of the layout container * @return Returns an Image containing the rendered document. */ public static BufferedImage renderToImage(String url, int width, int height) { return renderToImage(url, width, height, BufferedImage.TYPE_INT_ARGB); }
g2r = new Graphics2DRenderer(); g2r.getSharedContext().setPrint(true); g2r.getSharedContext().setInteractive(false); g2r.getSharedContext().setDPI(72f); g2r.getSharedContext().getTextRenderer().setSmoothingThreshold(0); g2r.getSharedContext().setUserAgentCallback(panel.getSharedContext().getUserAgentCallback()); g2r.setDocument(panel.getDocument(), panel.getSharedContext().getUac().getBaseURL()); g2r.getSharedContext().setReplacedElementFactory(panel.getSharedContext().getReplacedElementFactory()); g2r.layout(g2, null); g2r.getPanel().assignPagePrintPositions(g2); if (page >= g2r.getPanel().getRootLayer().getPages().size()) { return Printable.NO_SUCH_PAGE; g2r.getPanel().paintPage(g2, page);
/** * Renders the document to the given canvas. Call layout() first. * * @param g2 Canvas to render to. */ public void render(Graphics2D g2) { if (g2.getClip() == null) { g2.setClip(getMinimumSize()); } panel.paintComponent(g2); }
g2r = new Graphics2DRenderer(); g2r.getSharedContext().setPrint(true); g2r.getSharedContext().setInteractive(false); g2r.getSharedContext().setDPI(72f); g2r.getSharedContext().getTextRenderer().setSmoothingThreshold(0); g2r.getSharedContext().setUserAgentCallback(panel.getSharedContext().getUserAgentCallback()); g2r.setDocument(panel.getDocument(), panel.getSharedContext().getUac().getBaseURL()); g2r.getSharedContext().setReplacedElementFactory(panel.getSharedContext().getReplacedElementFactory()); g2r.layout(g2, null); g2r.getPanel().assignPagePrintPositions(g2); if (page >= g2r.getPanel().getRootLayer().getPages().size()) { return Printable.NO_SUCH_PAGE; g2r.getPanel().paintPage(g2, page);
/** * Renders the document to the given canvas. Call layout() first. * * @param g2 Canvas to render to. */ public void render(Graphics2D g2) { if (g2.getClip() == null) { g2.setClip(getMinimumSize()); } panel.paintComponent(g2); }
/** * A static utility method to automatically create an image from a * document. The buffered image type must be specified. * * @param url URL for the document to render. * @param width Width in pixels of the layout container * @param height Height in pixels of the layout container * @param bufferedImageType On of the pre-defined image types for a java.awt.image.BufferedImage, such * as TYPE_INT_ARGB or TYPE_INT_RGB. * @return Returns an Image containing the rendered document. */ public static BufferedImage renderToImage(String url, int width, int height, int bufferedImageType) { Graphics2DRenderer g2r = new Graphics2DRenderer(); g2r.setDocument(url); Dimension dim = new Dimension(width, height); BufferedImage buff = new BufferedImage((int) dim.getWidth(), (int) dim.getHeight(), bufferedImageType); Graphics2D g = (Graphics2D) buff.getGraphics(); g2r.layout(g, dim); g2r.render(g); g.dispose(); return buff; }
/** * A static utility method to automatically create an image from a * document, where height is determined based on document content. * To estimate a size before rendering, use {@link #setDocument(String)} * and then {@link #getMinimumSize()}. The rendered image supports transparency. * * @param url java.net.URL for the document to render. * @param width Width in pixels of the layout container * @return Returns an java.awt.Image containing the rendered document. */ public static BufferedImage renderToImageAutoSize(String url, int width){ return renderToImageAutoSize(url, width, BufferedImage.TYPE_INT_ARGB); }
/** * A static utility method to automatically create an image from a * document; the image supports transparency. To render an image that does not support transparency, * use the overloaded version of this method {@link #renderToImage(String, int, int, int)}. * * @param url URL for the document to render. * @param width Width in pixels of the layout container * @param height Height in pixels of the layout container * @return Returns an Image containing the rendered document. */ public static BufferedImage renderToImage(String url, int width, int height) { return renderToImage(url, width, height, BufferedImage.TYPE_INT_ARGB); }
/** * A static utility method to automatically create an image from a * document. The buffered image type must be specified. * * @param url URL for the document to render. * @param width Width in pixels of the layout container * @param height Height in pixels of the layout container * @param bufferedImageType On of the pre-defined image types for a java.awt.image.BufferedImage, such * as TYPE_INT_ARGB or TYPE_INT_RGB. * @return Returns an Image containing the rendered document. */ public static BufferedImage renderToImage(String url, int width, int height, int bufferedImageType) { Graphics2DRenderer g2r = new Graphics2DRenderer(); g2r.setDocument(url); Dimension dim = new Dimension(width, height); BufferedImage buff = new BufferedImage((int) dim.getWidth(), (int) dim.getHeight(), bufferedImageType); Graphics2D g = (Graphics2D) buff.getGraphics(); g2r.layout(g, dim); g2r.render(g); g.dispose(); return buff; }
private void exportToImage(File htmlFile, String suffix, String format) throws Exception { final int defaultWidth = 1024; File bakupFile = generateBackupFile(htmlFile); String outputFile = this.getOutputDirectory() + File.separator + this.getOutputName() + "-" + suffix + "." + format; BufferedImage buff = null; buff = Graphics2DRenderer.renderToImageAutoSize(bakupFile.toURI().toURL().toString(), defaultWidth); FSImageWriter imageWriter = new FSImageWriter(); imageWriter.write(buff, outputFile); }
Graphics2DRenderer g2r = new Graphics2DRenderer(); g2r.setDocument(url); Dimension dim = new Dimension(width, 1000); g2r.layout(g, new Dimension(width, 1000)); g.dispose(); Rectangle rect = g2r.getMinimumSize(); g2r.render(g); g.dispose();
Graphics2DRenderer g2r = new Graphics2DRenderer(); g2r.setDocument(url); Dimension dim = new Dimension(width, 1000); g2r.layout(g, new Dimension(width, 1000)); g.dispose(); Rectangle rect = g2r.getMinimumSize(); g2r.render(g); g.dispose();
@Override public BufferedImage getBufferedImage(int imageType) { final Document document = documentHolder.getDocument(); if (bufferedImage != null || cacheImageType != imageType || cacheDocument != document) { cacheImageType = imageType; cacheDocument = document; Graphics2DRenderer renderer = new Graphics2DRenderer(); renderer.setDocument(document, document.getDocumentURI()); Dimension dimension = new Dimension(width, height); bufferedImage = new BufferedImage(width, height, imageType); if (autoHeight) { // do layout with temp buffer Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics(); renderer.layout(graphics2D, new Dimension(width, height)); graphics2D.dispose(); Rectangle size = renderer.getMinimumSize(); final int autoWidth = (int) size.getWidth(); final int autoHeight = (int) size.getHeight(); bufferedImage = new BufferedImage(autoWidth, autoHeight, imageType); dimension = new Dimension(autoWidth, autoHeight); } Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics(); renderer.layout(graphics2D, dimension); renderer.render(graphics2D); rootBox = renderer.getPanel().getRootBox(); graphics2D.dispose(); } return bufferedImage; }
int width = options.getW(); int height = options.getH() == null ? 1024 : options.getH(); Graphics2DRenderer renderer = new Graphics2DRenderer(); renderer.setDocument(options.getDocument(), options.getDocument().getDocumentURI()); renderer.layout(graphics2D, new Dimension(width, height)); graphics2D.dispose(); Rectangle size = renderer.getMinimumSize(); final int autoWidth = options.isAutoW() ? (int) size.getWidth() : width; final int autoHeight = (int) size.getHeight(); renderer.layout(graphics2D, dimension); renderer.render(graphics2D); graphics2D.dispose(); return bufferedImage;
int width = options.getW(); int height = options.getH() == null ? 1024 : options.getH(); Graphics2DRenderer renderer = new Graphics2DRenderer(); renderer.setDocument(options.getDocument(), options.getDocument().getDocumentURI()); renderer.layout(graphics2D, new Dimension(width, height)); graphics2D.dispose(); Rectangle size = renderer.getMinimumSize(); final int autoWidth = options.isAutoW() ? (int) size.getWidth() : width; final int autoHeight = (int) size.getHeight(); renderer.layout(graphics2D, dimension); renderer.render(graphics2D); graphics2D.dispose(); return bufferedImage;