List of usage examples for org.apache.pdfbox.util Matrix createAffineTransform
public AffineTransform createAffineTransform()
From source file:org.apache.pdflens.views.pagesview.PageDrawer.java
License:Apache License
/** * You should override this method if you want to perform an action when a * text is being processed. // w w w .ja v a 2 s. c om * * @param text The text to process */ protected void processTextPosition(TextPosition text) { //should use colorspaces for the font color but for now assume that //the font color is black try { if (this.getGraphicsState().getTextState().getRenderingMode() == PDTextState.RENDERING_MODE_FILL_TEXT) { graphics.setColor(this.getGraphicsState().getNonStrokingColor().getJavaColor()); } else if (this.getGraphicsState().getTextState() .getRenderingMode() == PDTextState.RENDERING_MODE_STROKE_TEXT) { graphics.setColor(this.getGraphicsState().getStrokingColor().getJavaColor()); } else { // TODO : need to implement.... log.warn("Unsupported RenderingMode " + this.getGraphicsState().getTextState().getRenderingMode() + " in PageDrawer.processTextPosition()." + " Using RenderingMode " + PDTextState.RENDERING_MODE_FILL_TEXT + " instead"); graphics.setColor(this.getGraphicsState().getNonStrokingColor().getJavaColor()); } PDFont font = text.getFont(); Matrix textPos = text.getTextPos().copy(); float x = textPos.getXPosition(); // the 0,0-reference has to be moved from the lower left (PDF) to the upper left (AWT-graphics) float y = pageSize.height - textPos.getYPosition(); // Set translation to 0,0. We only need the scaling and shearing textPos.setValue(2, 0, 0); textPos.setValue(2, 1, 0); // because of the moved 0,0-reference, we have to shear in the opposite direction textPos.setValue(0, 1, (-1) * textPos.getValue(0, 1)); textPos.setValue(1, 0, (-1) * textPos.getValue(1, 0)); AffineTransform at = textPos.createAffineTransform(); graphics.setClip(getGraphicsState().getCurrentClippingPath()); font.drawString(text.getCharacter(), graphics, text.getFontSize(), at, x, y); } catch (IOException io) { io.printStackTrace(); } }
From source file:org.fit.pdfdom.PDFBoxTree.java
License:Open Source License
protected void processImageOperation(List<COSBase> arguments) throws IOException { COSName objectName = (COSName) arguments.get(0); PDXObject xobject = getResources().getXObject(objectName); if (xobject instanceof PDImageXObject) { PDImageXObject pdfImage = (PDImageXObject) xobject; BufferedImage outputImage = pdfImage.getImage(); // x, y and size are handled by css attributes but still need to rotate the image so pulling // only rotation out of the matrix so no giant whitespace offset from translations Matrix ctm = getGraphicsState().getCurrentTransformationMatrix(); AffineTransform tr = ctm.createAffineTransform(); double rotate = Math.atan2(tr.getShearY(), tr.getScaleY()) - Math.toRadians(pdpage.getRotation()); outputImage = ImageUtils.rotateImage(outputImage, rotate); byte[] imageData = getImageData(outputImage); Rectangle2D imageBounds = pdfImage.getImage().getRaster().getBounds(); AffineTransform pageTransform = createCurrentPageTransformation(); AffineTransform imageTransform = new AffineTransform(ctm.createAffineTransform()); imageTransform.scale(1.0 / pdfImage.getWidth(), -1.0 / pdfImage.getHeight()); imageTransform.translate(0, -pdfImage.getHeight()); pageTransform.concatenate(imageTransform); Rectangle2D bounds = pageTransform.createTransformedShape(imageBounds).getBounds2D(); float x = (float) bounds.getX(); float y = (float) bounds.getY(); renderImage(x, y, (float) bounds.getWidth(), (float) bounds.getHeight(), "image/png", imageData); }/* w w w .jav a 2 s .c o m*/ }
From source file:org.xmlcml.pdf2svg.PDFPage2SVGConverter.java
License:Apache License
private void captureAndIndexGlyphVector() { String key = charname;//from ww w. j av a2 s .c o m if (key == null) { key = "" + charCode; } String pathString = amiFont.getPathStringByCharnameMap().get(key); LOG.trace("charname: " + charname + " path: " + pathString); if (pathString == null) { ensurePageSize(); PDFGraphics2D graphics = new PDFGraphics2D(amiFont); Matrix textPos = textPosition.getTextPos().copy(); float x = textPos.getXPosition(); // the 0,0-reference has to be moved from the lower left (PDF) to // the upper left (AWT-graphics) float y = pageSize.height - textPos.getYPosition(); // Set translation to 0,0. We only need the scaling and shearing textPos.setValue(2, 0, 0); textPos.setValue(2, 1, 0); // because of the moved 0,0-reference, we have to shear in the // opposite direction textPos.setValue(0, 1, (-1) * textPos.getValue(0, 1)); textPos.setValue(1, 0, (-1) * textPos.getValue(1, 0)); AffineTransform at = textPos.createAffineTransform(); PDMatrix fontMatrix = pdFont.getFontMatrix(); // matrix is r00 r01 r10 r11 t0 t1 double r00 = fontMatrix.getValue(0, 0) * 1000f; double r11 = fontMatrix.getValue(1, 1) * 1000f; LOG.trace("scales: " + r00 + "/" + r11); at.scale(r00, r11); // TODO setClip() is a massive performance hot spot. Investigate // optimization possibilities if (graphicsState == null) { LOG.debug("NULL graphics state"); // return; } else { graphics.setClip(graphicsState.getCurrentClippingPath()); } // the fontSize is no longer needed as it is already part of the // transformation // we should remove it from the parameter list in the long run try { pdFont.drawString(textPosition.getCharacter(), textPosition.getCodePoints(), graphics, 1, at, x, y); } catch (IOException e) { throw new RuntimeException("font.drawString", e); } pathString = graphics.getCurrentPathString(); LOG.trace(charname + ": created " + pathString); amiFont.getPathStringByCharnameMap().put(key, pathString); } LOG.trace("pathString: " + pathString); }