File pdfFile = new File("/path/to/pdf.pdf"); RandomAccessFile raf = new RandomAccessFile(pdfFile, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdf = new PDFFile(buf); PDFPage page = pdf.getPage(0); // create the image Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); Image image = page.getImage(rect.width, rect.height, // width & height rect, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done ); Graphics2D bufImageGraphics = bufferedImage.createGraphics(); bufImageGraphics.drawImage(image, 0, 0, null); ImageIO.write(bufferedImage, format, new File( "/path/to/image.jpg" ));
cmds.addPush(); } else if (cmd.equals("Q")) { processQCmd(); cmds.addXform(xform); } else if (cmd.equals("w")) { cmds.addStrokeWidth(popFloat()); } else if (cmd.equals("J")) { cmds.addEndCap(popInt()); } else if (cmd.equals("j")) { cmds.addLineJoin(popInt()); } else if (cmd.equals("M")) { cmds.addMiterLimit(popInt()); } else if (cmd.equals("d")) { cmds.addDash(dashary, phase); } else if (cmd.equals("ri")) { cmds.addPath(path, PDFShapeCmd.STROKE | clip); clip = 0; path = new GeneralPath(); cmds.addPath(path, PDFShapeCmd.STROKE | clip); clip = 0; path = new GeneralPath();
cmds.addStrokeWidth(d.getFloatValue()); cmds.addEndCap(d.getIntValue()); cmds.addLineJoin(d.getIntValue()); cmds.addMiterLimit(d.getFloatValue()); dash[i] = pdash[i].getFloatValue(); cmds.addDash(dash, d.getAt(1).getFloatValue()); cmds.addStrokeAlpha(d.getFloatValue()); cmds.addFillAlpha(d.getFloatValue());
formCmds = new PDFPage(bbox, 0); formCmds.addXform(at); cmds.addPush(); cmds.addCommands(formCmds); cmds.addPop();
/** * set the current path * @param path the path * @param style the style: PDFShapeCmd.STROKE, PDFShapeCmd.FILL, * PDFShapeCmd.BOTH, PDFShapeCmd.CLIP, or some combination. */ public void addPath(GeneralPath path, int style) { addCommand(new PDFShapeCmd(path, style)); }
final Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox() .getHeight()); final int rotation = page.getRotation(); Rectangle rect1 = rect; if (rotation == 90 || rotation == 270) { final Image img = page.getImage(rect.width, rect.height, rect1, // clip
final PDFPage page = new PDFPage(getBBox(), 0); page.addFillPaint(basePaint); page.addStrokePaint(basePaint);
/** * build a shader from a dictionary. */ private void doShader(PDFObject shaderObj) throws IOException { PDFShader shader = PDFShader.getShader(shaderObj, resources); cmds.addPush(); Rectangle2D bbox = shader.getBBox(); if (bbox != null) { cmds.addFillPaint(shader.getPaint()); cmds.addPath(new GeneralPath(bbox), PDFShapeCmd.FILL); } cmds.addPop(); }
/** * Get an image producer which can be used to draw the image * represented by this PDFPage. The ImageProducer is guaranteed to * stay in sync with the PDFPage as commands are added to it. * * The image will contain the section of the page specified by the clip, * scaled to fit in the area given by width and height. * * @param width the width of the image to be produced * @param height the height of the image to be produced * @param clip the region in <b>page space</b> of the entire page to * display * @param observer an image observer who will be notified when the * image changes, or null * @return an Image that contains the PDF data */ public Image getImage(int width, int height, Rectangle2D clip, ImageObserver observer) { return getImage(width, height, clip, observer, true, false); }
currentPage.stop(prevSize.width, prevSize.height, prevClip); Dimension pageSize = page.getUnstretchedSize(sz.width, sz.height, useClip); currentImage = page.getImage(pageSize.width, pageSize.height, useClip, this); currentXform = page.getInitialTransform(pageSize.width, pageSize.height, useClip);
Rectangle2D clip) { AffineTransform at = new AffineTransform(); switch (getRotation()) { case 0: at = new AffineTransform(1, 0, 0, -1, 0, height); clip = getBBox(); } else if (getRotation() == 90 || getRotation() == 270) { int tmp = width; width = height;
/** Add commands for this glyph to a page */ public Point2D addCommands(PDFPage cmds, AffineTransform transform, int mode) { if (shape != null) { GeneralPath outline= (GeneralPath) shape.createTransformedShape(transform); cmds.addCommand(new PDFShapeCmd(outline, mode)); } else if (page != null) { cmds.addCommands(page, transform); } return advance; }
/** * Draw a page */ private void drawPage(Graphics2D g2, PDFPage page, int w, int h) { // System.out.println("Drawing page in: " + w + " x " + h); Dimension pageSize = page.getUnstretchedSize(w - 20, h - 20, null); Image image = page.getImage(pageSize.width, pageSize.height, null, null, true, true); g2.drawImage(image, 0, 0, null); }
/** * build a shader from a dictionary. */ private void doShader(PDFObject shaderObj) throws IOException { PDFShader shader = PDFShader.getShader(shaderObj, resources); // cmds.addPush(); Rectangle2D bbox = shader.getBBox(); if (bbox != null) { cmds.addFillPaint(shader.getPaint()); cmds.addPath(new GeneralPath(bbox), PDFShapeCmd.FILL); } else { cmds.addFillPaint(shader.getPaint()); cmds.addPath(null, PDFShapeCmd.FILL); } // cmds.addPop(); }
int wid = (int) Math.ceil((lineheight - border) * p.getAspectRatio()); Image i = p.getImage(wid, (lineheight - border), null, this, true, true);
private boolean doesRotationSwapDimensions() { return getRotation() == 90 || getRotation() == 270; }
switch (getRotation()) { case 0: at = new AffineTransform(1, 0, 0, -1, 0, height); break; default: throw new IllegalArgumentException("Non-quadrant rotation: " + getRotation()); double scaleX = (doesRotationSwapDimensions() ? height : width) / clip.getWidth(); double scaleY = (doesRotationSwapDimensions() ? width: height) / clip.getHeight(); at.scale(scaleX, scaleY);
/** * add a collection of commands to the page list. This is probably * invoked as the result of an XObject 'do' command, or through a * type 3 font. */ public void addCommands(PDFPage page) { addCommands(page, null); }
/** * Create a PDF Page object by finding the relevant inherited * properties * * @param pageObj the PDF object for the page to be created */ private PDFPage createPage(int pagenum, PDFObject pageObj) throws IOException { int rotation = 0; Rectangle2D mediabox = null; // second choice, if no crop Rectangle2D cropbox = null; // first choice PDFObject mediaboxObj = getInheritedValue(pageObj, "MediaBox"); if (mediaboxObj != null) { mediabox = parseNormalisedRectangle(mediaboxObj); } PDFObject cropboxObj = getInheritedValue(pageObj, "CropBox"); if (cropboxObj != null) { cropbox = parseNormalisedRectangle(cropboxObj); } PDFObject rotateObj = getInheritedValue(pageObj, "Rotate"); if (rotateObj != null) { rotation = rotateObj.getIntValue(); } Rectangle2D bbox = ((cropbox == null) ? mediabox : cropbox); return new PDFPage(pagenum, bbox, rotation, cache); }