float width = mediabox.getWidth() - 2*margin; float startX = mediabox.getLowerLeftX() + margin; float startY = mediabox.getUpperRightY() - margin;
float width = mediabox.getWidth() - 2*margin; float startX = mediabox.getLowerLeftX() + margin; float startY = mediabox.getUpperRightY() - margin;
/** * This will get the height of this rectangle as calculated by * upperRightY - lowerLeftY. * * @return The height of this rectangle. */ public float getHeight() { return getUpperRightY() - getLowerLeftY(); }
protected boolean isNonZeroBoundingBox (PDRectangle bbox) { return bbox != null && ( Float.compare(bbox.getLowerLeftX(), 0) != 0 || Float.compare(bbox.getLowerLeftY(), 0) != 0 || Float.compare(bbox.getUpperRightX(), 0) != 0 || Float.compare(bbox.getUpperRightY(), 0) != 0 ); }
/** * Returns a general path equivalent to this rectangle. This method avoids the problems * caused by Rectangle2D not working well with -ve rectangles. */ public GeneralPath toGeneralPath() { float x1 = getLowerLeftX(); float y1 = getLowerLeftY(); float x2 = getUpperRightX(); float y2 = getUpperRightY(); GeneralPath path = new GeneralPath(); path.moveTo(x1, y1); path.lineTo(x2, y1); path.lineTo(x2, y2); path.lineTo(x1, y2); path.closePath(); return path; }
private AffineTransform calculateMatrix(PDRectangle bbox, int rotation) { if (rotation == 0) { return new AffineTransform(); } float tx = 0, ty = 0; switch (rotation) { case 90: tx = bbox.getUpperRightY(); break; case 180: tx = bbox.getUpperRightY(); ty = bbox.getUpperRightX(); break; case 270: ty = bbox.getUpperRightX(); break; default: break; } Matrix matrix = Matrix.getRotateInstance(Math.toRadians(rotation), tx, ty); return matrix.createAffineTransform(); }
/** * This will return a string representation of this rectangle. * * @return This object as a string. */ @Override public String toString() { return "[" + getLowerLeftX() + "," + getLowerLeftY() + "," + getUpperRightX() + "," + getUpperRightY() +"]"; } }
/** * Method to determine if the x/y point is inside this rectangle. * @param x The x-coordinate to test. * @param y The y-coordinate to test. * @return True if the point is inside this rectangle. */ public boolean contains( float x, float y ) { float llx = getLowerLeftX(); float urx = getUpperRightX(); float lly = getLowerLeftY(); float ury = getUpperRightY(); return x >= llx && x <= urx && y >= lly && y <= ury; }
/** * Clips the given box to the bounds of the media box. */ private PDRectangle clipToMediaBox(PDRectangle box) { PDRectangle mediaBox = getMediaBox(); PDRectangle result = new PDRectangle(); result.setLowerLeftX(Math.max(mediaBox.getLowerLeftX(), box.getLowerLeftX())); result.setLowerLeftY(Math.max(mediaBox.getLowerLeftY(), box.getLowerLeftY())); result.setUpperRightX(Math.min(mediaBox.getUpperRightX(), box.getUpperRightX())); result.setUpperRightY(Math.min(mediaBox.getUpperRightY(), box.getUpperRightY())); return result; }
private BoundingBox generateBoundingBox() throws IOException { if (getFontDescriptor() != null) { PDRectangle bbox = getFontDescriptor().getFontBoundingBox(); if (bbox != null && (Float.compare(bbox.getLowerLeftX(), 0) != 0 || Float.compare(bbox.getLowerLeftY(), 0) != 0 || Float.compare(bbox.getUpperRightX(), 0) != 0 || Float.compare(bbox.getUpperRightY(), 0) != 0)) { return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getUpperRightX(), bbox.getUpperRightY()); } } return ttf.getFontBBox(); }
private void calcBBox(PDType3Font font) throws IOException { double minX = 0; double maxX = 0; double minY = 0; double maxY = 0; for (int index = 0; index <= 255; ++index) { PDType3CharProc charProc = font.getCharProc(index); if (charProc == null) { continue; } PDRectangle glyphBBox = charProc.getGlyphBBox(); if (glyphBBox == null) { continue; } minX = Math.min(minX, glyphBBox.getLowerLeftX()); maxX = Math.max(maxX, glyphBBox.getUpperRightX()); minY = Math.min(minY, glyphBBox.getLowerLeftY()); maxY = Math.max(maxY, glyphBBox.getUpperRightY()); } fontBBox = new PDRectangle((float) minX, (float) minY, (float) (maxX - minX), (float) (maxY - minY)); }
private BoundingBox generateBoundingBox() { if (getFontDescriptor() != null) { PDRectangle bbox = getFontDescriptor().getFontBoundingBox(); if (bbox != null && (Float.compare(bbox.getLowerLeftX(),0) != 0 || Float.compare(bbox.getLowerLeftY(),0) != 0 || Float.compare(bbox.getUpperRightX(),0) != 0 || Float.compare(bbox.getUpperRightY(),0) != 0)) { return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getUpperRightX(), bbox.getUpperRightY()); } } if (cidFont != null) { return cidFont.getFontBBox(); } else { try { return t1Font.getFontBBox(); } catch (IOException e) { LOG.debug("Couldn't get font bounding box - returning default value", e); return new BoundingBox(); } } }
/** * Returns a path which represents this rectangle having been transformed by the given matrix. * Note that the resulting path need not be rectangular. */ public GeneralPath transform(Matrix matrix) { float x1 = getLowerLeftX(); float y1 = getLowerLeftY(); float x2 = getUpperRightX(); float y2 = getUpperRightY(); Point2D.Float p0 = matrix.transformPoint(x1, y1); Point2D.Float p1 = matrix.transformPoint(x2, y1); Point2D.Float p2 = matrix.transformPoint(x2, y2); Point2D.Float p3 = matrix.transformPoint(x1, y2); GeneralPath path = new GeneralPath(); path.moveTo(p0.getX(), p0.getY()); path.lineTo(p1.getX(), p1.getY()); path.lineTo(p2.getX(), p2.getY()); path.lineTo(p3.getX(), p3.getY()); path.closePath(); return path; }
/** * Returns the updated <code>RD</code> entry for Square and Circle annotations. * * @return Annotation <code>RD</code> value. */ PDRectangle getRectDifference() { if (annotRect == null) { float d = (float)lineWidth / 2; return new PDRectangle(d, d, (float)lineWidth, (float)lineWidth); } PDRectangle re = (rectWithDiff != null) ? rectWithDiff : annotRect; float left = re.getLowerLeftX() - (float)bboxMinX; float bottom = re.getLowerLeftY() - (float)bboxMinY; float right = (float)bboxMaxX - re.getUpperRightX(); float top = (float)bboxMaxY - re.getUpperRightY(); return new PDRectangle(left, bottom, right - left, top - bottom); }
/** * Creates a cloudy border for a Circle annotation. * The ellipse is specified by the <code>RD</code> entry and the * <code>Rect</code> entry that was passed in to the constructor. * * @param rd entry <code>RD</code>, or null if the entry does not exist * @throws IOException If there is an error writing to the stream. */ void createCloudyEllipse(PDRectangle rd) throws IOException { rectWithDiff = applyRectDiff(rd, 0); double left = rectWithDiff.getLowerLeftX(); double bottom = rectWithDiff.getLowerLeftY(); double right = rectWithDiff.getUpperRightX(); double top = rectWithDiff.getUpperRightY(); cloudyEllipseImpl(left, bottom, right, top); finish(); }
private BoundingBox generateBoundingBox() throws IOException { if (getFontDescriptor() != null) { PDRectangle bbox = getFontDescriptor().getFontBoundingBox(); if (bbox != null) { return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getUpperRightX(), bbox.getUpperRightY()); } } return ttf.getFontBBox(); }
/** * Creates a cloudy border for a rectangular annotation. * The rectangle is specified by the <code>RD</code> entry and the * <code>Rect</code> entry that was passed in to the constructor. * <p> * This can be used for Square and FreeText annotations. However, this does * not produce the text and the callout line for FreeTexts. * * @param rd entry <code>RD</code>, or null if the entry does not exist * @throws IOException If there is an error writing to the stream. */ void createCloudyRectangle(PDRectangle rd) throws IOException { rectWithDiff = applyRectDiff(rd, lineWidth / 2); double left = rectWithDiff.getLowerLeftX(); double bottom = rectWithDiff.getLowerLeftY(); double right = rectWithDiff.getUpperRightX(); double top = rectWithDiff.getUpperRightY(); cloudyRectangleImpl(left, bottom, right, top, false); finish(); }
private BoundingBox generateBoundingBox() throws IOException { if (getFontDescriptor() != null) { PDRectangle bbox = getFontDescriptor().getFontBoundingBox(); if (isNonZeroBoundingBox(bbox)) { return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getUpperRightX(), bbox.getUpperRightY()); } } return genericFont.getFontBBox(); }
private BoundingBox generateBoundingBox() throws IOException { if (getFontDescriptor() != null) { PDRectangle bbox = getFontDescriptor().getFontBoundingBox(); if (isNonZeroBoundingBox(bbox)) { return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getUpperRightX(), bbox.getUpperRightY()); } } return genericFont.getFontBBox(); }
private PDRectangle adjustRectAndBBox(PDAnnotationText annotation, float width, float height) { // For /Note (other types have different values): // Adobe takes the left upper bound as anchor, and adjusts the rectangle to 18 x 20. // Observed with files 007071.pdf, 038785.pdf, 038787.pdf, // but not with 047745.pdf p133 and 084374.pdf p48, both have the NoZoom flag. // there the BBox is also set to fixed values, but the rectangle is left untouched. // When no flags are there, Adobe sets /F 24 = NoZoom NoRotate. PDRectangle rect = getRectangle(); PDRectangle bbox; if (!annotation.isNoZoom()) { rect.setUpperRightX(rect.getLowerLeftX() + width); rect.setLowerLeftY(rect.getUpperRightY() - height); annotation.setRectangle(rect); } if (!annotation.getCOSObject().containsKey(COSName.F)) { // We set these flags because Adobe does so, but PDFBox doesn't support them when rendering. annotation.setNoRotate(true); annotation.setNoZoom(true); } bbox = new PDRectangle(width, height); annotation.getNormalAppearanceStream().setBBox(bbox); return bbox; }