// renders a page to the given graphics public void renderPage( PDPage page, Paint paint, Canvas canvas, int width, int height, float scaleX, float scaleY) throws IOException { canvas.scale(scaleX, scaleY); // TODO should we be passing the scale to PageDrawer rather than messing with Graphics? PDRectangle cropBox = page.getCropBox(); int rotationAngle = page.getRotation(); if (rotationAngle != 0) { float translateX = 0; float translateY = 0; switch (rotationAngle) { case 90: translateX = cropBox.getHeight(); break; case 270: translateY = cropBox.getWidth(); break; case 180: translateX = cropBox.getWidth(); translateY = cropBox.getHeight(); break; } canvas.translate(translateX, translateY); canvas.rotate((float) Math.toRadians(rotationAngle)); } PageDrawer drawer = new PageDrawer(page); drawer.drawPage(paint, canvas, cropBox); }
/** * Returns the given page as an RGB image at the given scale. * * @param pageIndex the zero-based index of the page to be converted * @param scale the scaling factor, where 1 = 72 DPI * @param config the bitmap config to create * @return the rendered page image * @throws IOException if the PDF cannot be read */ public Bitmap renderImage(int pageIndex, float scale, Bitmap.Config config) throws IOException { PDPage page = document.getPage(pageIndex); PDRectangle cropbBox = page.getCropBox(); float widthPt = cropbBox.getWidth(); float heightPt = cropbBox.getHeight(); int widthPx = Math.round(widthPt * scale); int heightPx = Math.round(heightPt * scale); int rotationAngle = page.getRotation(); // swap width and height Bitmap image; if (rotationAngle == 90 || rotationAngle == 270) { image = Bitmap.createBitmap(heightPx, widthPx, config); } else { image = Bitmap.createBitmap(widthPx, heightPx, config); } // use a transparent background if the imageType supports alpha Paint paint = new Paint(); Canvas canvas = new Canvas(image); if (config != Bitmap.Config.ARGB_8888) { paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.FILL); canvas.drawRect(0, 0, image.getWidth(), image.getHeight(), paint); paint.reset(); } renderPage(page, paint, canvas, image.getWidth(), image.getHeight(), scale, scale); return image; }
/** * Returns the given page as an RGB or ARGB image at the given scale. * * @param pageIndex the zero-based index of the page to be converted * @param scale the scaling factor, where 1 = 72 DPI * @param imageType the type of image to return * @return the rendered page image * @throws IOException if the PDF cannot be read */ public BufferedImage renderImage(int pageIndex, float scale, ImageType imageType) throws IOException { PDPage page = document.getPage(pageIndex); PDRectangle cropbBox = page.getCropBox(); float widthPt = cropbBox.getWidth(); float heightPt = cropbBox.getHeight(); int widthPx = Math.round(widthPt * scale); int heightPx = Math.round(heightPt * scale); int rotationAngle = page.getRotation(); // swap width and height BufferedImage image; if (rotationAngle == 90 || rotationAngle == 270) { image = new BufferedImage(heightPx, widthPx, imageType.toBufferedImageType()); } else { image = new BufferedImage(widthPx, heightPx, imageType.toBufferedImageType()); } // use a transparent background if the imageType supports alpha Graphics2D g = image.createGraphics(); if (imageType == ImageType.ARGB) { g.setBackground(new Color(0, 0, 0, 0)); } else { g.setBackground(Color.WHITE); } renderPage(page, g, image.getWidth(), image.getHeight(), scale, scale); g.dispose(); return image; }
/** Initialises the stream engine for the given page. */ private void initPage(PDPage page) { if (page == null) { throw new IllegalArgumentException("Page cannot be null"); } currentPage = page; graphicsStack.clear(); graphicsStack.push(new PDGraphicsState(page.getCropBox())); textMatrix = null; textLineMatrix = null; resources = null; initialMatrix = page.getMatrix(); }
/** * Renders a given page to an AWT Graphics2D instance. * * @param pageIndex the zero-based index of the page to be converted * @param graphics the Graphics2D on which to draw the page * @param scale the scale to draw the page at * @throws IOException if the PDF cannot be read */ public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scale) throws IOException { PDPage page = document.getPage(pageIndex); // TODO need width/wight calculations? should these be in PageDrawer? PDRectangle adjustedCropBox = page.getCropBox(); renderPage( page, graphics, (int) adjustedCropBox.getWidth(), (int) adjustedCropBox.getHeight(), scale, scale); }
// renders a page to the given graphics private void renderPage( PDPage page, Graphics2D graphics, int width, int height, float scaleX, float scaleY) throws IOException { graphics.clearRect(0, 0, width, height); graphics.scale(scaleX, scaleY); // TODO should we be passing the scale to PageDrawer rather than messing with Graphics? PDRectangle cropBox = page.getCropBox(); int rotationAngle = page.getRotation(); if (rotationAngle != 0) { float translateX = 0; float translateY = 0; switch (rotationAngle) { case 90: translateX = cropBox.getHeight(); break; case 270: translateY = cropBox.getWidth(); break; case 180: translateX = cropBox.getWidth(); translateY = cropBox.getHeight(); break; } graphics.translate(translateX, translateY); graphics.rotate((float) Math.toRadians(rotationAngle)); } // the end-user may provide a custom PageDrawer PageDrawerParameters parameters = new PageDrawerParameters(this, page); PageDrawer drawer = createPageDrawer(parameters); drawer.drawPage(graphics, cropBox); }