Java Code Examples for org.apache.pdfbox.pdmodel.common.PDRectangle#getUpperRightX()

The following examples show how to use org.apache.pdfbox.pdmodel.common.PDRectangle#getUpperRightX() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDCIDFontType2.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
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();
}
 
Example 2
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 6 votes vote down vote up
/**
    * Transform the rectangle in order to match the page rotation
    * @param rect the rectangle.
    * @param page the page.
    * @return the transformed rectangle.
    */
   public static PDRectangle transformToPageRotation(
    final PDRectangle rect, final PDPage page) {
AffineTransform transform = transformToPageRotation(page);
if (transform == null) {
    return rect;
}
float[] points = new float[] {rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getUpperRightX(), rect.getUpperRightY()};
float[] rotatedPoints = new float[4]; 
transform.transform(points, 0, rotatedPoints, 0, 2);
PDRectangle rotated = new PDRectangle();
rotated.setLowerLeftX(rotatedPoints[0]);
rotated.setLowerLeftY(rotatedPoints[1]);
rotated.setUpperRightX(rotatedPoints[2]);
rotated.setUpperRightY(rotatedPoints[3]);
return rotated;
   }
 
Example 3
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 6 votes vote down vote up
/**
    * Transform the rectangle in order to match the page rotation
    * @param rect the rectangle.
    * @param page the page.
    * @return the transformed rectangle.
    */
   public static PDRectangle transformToPageRotation(
    final PDRectangle rect, final PDPage page) {
AffineTransform transform = transformToPageRotation(page);
if (transform == null) {
    return rect;
}
float[] points = new float[] {rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getUpperRightX(), rect.getUpperRightY()};
float[] rotatedPoints = new float[4]; 
transform.transform(points, 0, rotatedPoints, 0, 2);
PDRectangle rotated = new PDRectangle();
rotated.setLowerLeftX(rotatedPoints[0]);
rotated.setLowerLeftY(rotatedPoints[1]);
rotated.setUpperRightX(rotatedPoints[2]);
rotated.setUpperRightY(rotatedPoints[3]);
return rotated;
   }
 
Example 4
Source File: RotatePageContent.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/40611736/rotate-pdf-around-its-center-using-pdfbox-in-java">
 * Rotate PDF around its center using PDFBox in java
 * </a>
 * <p>
 * This test shows how to rotate the page content and then move its crop box and
 * media box accordingly to make it appear as if the content was rotated around
 * the center of the crop box.
 * </p>
 */
@Test
public void testRotateMoveBox() throws IOException
{
    try (   InputStream resource = getClass().getResourceAsStream("IRJET_Copy_Right_form.pdf")  )
    {
        PDDocument document = Loader.loadPDF(resource);
        PDPage page = document.getDocumentCatalog().getPages().get(0);
        PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false);
        Matrix matrix = Matrix.getRotateInstance(Math.toRadians(45), 0, 0);
        cs.transform(matrix);
        cs.close();

        PDRectangle cropBox = page.getCropBox();
        float cx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
        float cy = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
        Point2D.Float newC = matrix.transformPoint(cx, cy);
        float tx = (float)newC.getX() - cx;
        float ty = (float)newC.getY() - cy;
        page.setCropBox(new PDRectangle(cropBox.getLowerLeftX() + tx, cropBox.getLowerLeftY() + ty, cropBox.getWidth(), cropBox.getHeight()));
        PDRectangle mediaBox = page.getMediaBox();
        page.setMediaBox(new PDRectangle(mediaBox.getLowerLeftX() + tx, mediaBox.getLowerLeftY() + ty, mediaBox.getWidth(), mediaBox.getHeight()));

        document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-move-box.pdf"));
    }
}
 
Example 5
Source File: RotatePageContent.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/40611736/rotate-pdf-around-its-center-using-pdfbox-in-java">
 * Rotate PDF around its center using PDFBox in java
 * </a>
 * <p>
 * This test shows how to rotate the page content around the center of its crop box
 * and then crop it to make all previously visible content fit.
 * </p>
 */
@Test
public void testRotateCenterScale() throws IOException
{
    try (   InputStream resource = getClass().getResourceAsStream("IRJET_Copy_Right_form.pdf")  )
    {
        PDDocument document = Loader.loadPDF(resource);
        PDPage page = document.getDocumentCatalog().getPages().get(0);
        PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false);

        Matrix matrix = Matrix.getRotateInstance(Math.toRadians(45), 0, 0);
        PDRectangle cropBox = page.getCropBox();
        float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
        float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;

        Rectangle rectangle = cropBox.transform(matrix).getBounds();
        float scale = Math.min(cropBox.getWidth() / (float)rectangle.getWidth(), cropBox.getHeight() / (float)rectangle.getHeight());

        cs.transform(Matrix.getTranslateInstance(tx, ty));
        cs.transform(matrix);
        cs.transform(Matrix.getScaleInstance(scale, scale));
        cs.transform(Matrix.getTranslateInstance(-tx, -ty));
        cs.close();
        document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-center-scale.pdf"));
    }
}
 
Example 6
Source File: RotatePageContent.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/40611736/rotate-pdf-around-its-center-using-pdfbox-in-java">
 * Rotate PDF around its center using PDFBox in java
 * </a>
 * <p>
 * This test shows how to rotate the page content around the center of its crop box.
 * </p>
 */
@Test
public void testRotateCenter() throws IOException
{
    try (   InputStream resource = getClass().getResourceAsStream("IRJET_Copy_Right_form.pdf")  )
    {
        PDDocument document = Loader.loadPDF(resource);
        PDPage page = document.getDocumentCatalog().getPages().get(0);
        PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false); 
        PDRectangle cropBox = page.getCropBox();
        float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
        float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
        cs.transform(Matrix.getTranslateInstance(tx, ty));
        cs.transform(Matrix.getRotateInstance(Math.toRadians(45), 0, 0));
        cs.transform(Matrix.getTranslateInstance(-tx, -ty));
        cs.close();
        document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-center.pdf"));
    }
}
 
Example 7
Source File: CloudyBorder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 8
Source File: AppearanceGeneratorHelper.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
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();

}
 
Example 9
Source File: PDCIDFontType0.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private BoundingBox generateBoundingBox()
{
    if (getFontDescriptor() != null) {
        PDRectangle bbox = getFontDescriptor().getFontBoundingBox();
        if (bbox.getLowerLeftX() != 0 || bbox.getLowerLeftY() != 0 ||
            bbox.getUpperRightX() != 0 || bbox.getUpperRightY() != 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)
        {
            return new BoundingBox();
        }
    }
}
 
Example 10
Source File: PDType1CFont.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private BoundingBox generateBoundingBox() throws IOException
{
    if (getFontDescriptor() != null) {
        PDRectangle bbox = getFontDescriptor().getFontBoundingBox();
        if (bbox != null
                && (bbox.getLowerLeftX() != 0 || bbox.getLowerLeftY() != 0
                || bbox.getUpperRightX() != 0 || bbox.getUpperRightY() != 0))
        {
            return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(),
                                   bbox.getUpperRightX(), bbox.getUpperRightY());
        }
    }
    return genericFont.getFontBBox();
}
 
Example 11
Source File: PDType3Font.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private BoundingBox generateBoundingBox()
{
    PDRectangle rect = getFontBBox();
    if (rect.getLowerLeftX() == 0 && rect.getLowerLeftY() == 0
            && rect.getUpperRightX() == 0 && rect.getUpperRightY() == 0)
    {
        // Plan B: get the max bounding box of the glyphs
        COSDictionary cp = getCharProcs();
        for (COSName name : cp.keySet())
        {
            COSBase base = cp.getDictionaryObject(name);
            if (base instanceof COSStream)
            {
                PDType3CharProc charProc = new PDType3CharProc(this, (COSStream) base);
                try
                {
                    PDRectangle glyphBBox = charProc.getGlyphBBox();
                    if (glyphBBox == null)
                    {
                        continue;
                    }
                    rect.setLowerLeftX(Math.min(rect.getLowerLeftX(), glyphBBox.getLowerLeftX()));
                    rect.setLowerLeftY(Math.min(rect.getLowerLeftY(), glyphBBox.getLowerLeftY()));
                    rect.setUpperRightX(Math.max(rect.getUpperRightX(), glyphBBox.getUpperRightX()));
                    rect.setUpperRightY(Math.max(rect.getUpperRightY(), glyphBBox.getUpperRightY()));
                }
                catch (IOException ex)
                {
                    // ignore
                }
            }
        }
    }
    return new BoundingBox(rect.getLowerLeftX(), rect.getLowerLeftY(),
            rect.getUpperRightX(), rect.getUpperRightY());
}
 
Example 12
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
/**
    * Return the quad points representation of the given rect.
    * 
    * @param rect
    *            the rectangle.
    * @param xOffset
    *            the offset in x-direction to add.
    * @param yOffset
    *            the offset in y-direction to add.
    * @return the quad points.
    */
   public static float[] toQuadPoints(final PDRectangle rect, float xOffset,
    float yOffset) {
float[] quads = new float[8];
quads[0] = rect.getLowerLeftX() + xOffset; // x1
quads[1] = rect.getUpperRightY() + yOffset; // y1
quads[2] = rect.getUpperRightX() + xOffset; // x2
quads[3] = quads[1]; // y2
quads[4] = quads[0]; // x3
quads[5] = rect.getLowerLeftY() + yOffset; // y3
quads[6] = quads[2]; // x4
quads[7] = quads[5]; // y5
return quads;
   }
 
Example 13
Source File: PDType1Font.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private BoundingBox generateBoundingBox() throws IOException
{
    if (getFontDescriptor() != null) {
        PDRectangle bbox = getFontDescriptor().getFontBoundingBox();
        if (bbox != null &&
                (bbox.getLowerLeftX() != 0 || bbox.getLowerLeftY() != 0 ||
                 bbox.getUpperRightX() != 0 || bbox.getUpperRightY() != 0))
        {
            return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(),
                                   bbox.getUpperRightX(), bbox.getUpperRightY());
        }
    }
    return genericFont.getFontBBox();
}
 
Example 14
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
/**
    * Return the quad points representation of the given rect.
    * 
    * @param rect
    *            the rectangle.
    * @param xOffset
    *            the offset in x-direction to add.
    * @param yOffset
    *            the offset in y-direction to add.
    * @return the quad points.
    */
   public static float[] toQuadPoints(final PDRectangle rect, float xOffset,
    float yOffset) {
float[] quads = new float[8];
quads[0] = rect.getLowerLeftX() + xOffset; // x1
quads[1] = rect.getUpperRightY() + yOffset; // y1
quads[2] = rect.getUpperRightX() + xOffset; // x2
quads[3] = quads[1]; // y2
quads[4] = quads[0]; // x3
quads[5] = rect.getLowerLeftY() + yOffset; // y3
quads[6] = quads[2]; // x4
quads[7] = quads[5]; // y5
return quads;
   }
 
Example 15
Source File: PDTrueTypeFont.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
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();
}
 
Example 16
Source File: PDCircleAppearanceHandler.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void generateNormalAppearance()
{
    float lineWidth = getLineWidth();
    PDAnnotationSquareCircle annotation = (PDAnnotationSquareCircle) getAnnotation();
    PDAppearanceContentStream contentStream  = null;

    try
    {
        contentStream = getNormalAppearanceAsContentStream();
        boolean hasStroke = contentStream.setStrokingColorOnDemand(getColor());
        boolean hasBackground = contentStream
                .setNonStrokingColorOnDemand(annotation.getInteriorColor());

        setOpacity(contentStream, annotation.getConstantOpacity());

        contentStream.setBorderLine(lineWidth, annotation.getBorderStyle(), annotation.getBorder());
        PDBorderEffectDictionary borderEffect = annotation.getBorderEffect();

        if (borderEffect != null && borderEffect.getStyle().equals(PDBorderEffectDictionary.STYLE_CLOUDY))
        {
            CloudyBorder cloudyBorder = new CloudyBorder(contentStream,
                borderEffect.getIntensity(), lineWidth, getRectangle());
            cloudyBorder.createCloudyEllipse(annotation.getRectDifference());
            annotation.setRectangle(cloudyBorder.getRectangle());
            annotation.setRectDifference(cloudyBorder.getRectDifference());
            PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
            appearanceStream.setBBox(cloudyBorder.getBBox());
            appearanceStream.setMatrix(cloudyBorder.getMatrix());
        }
        else
        {
            // Acrobat applies a padding to each side of the bbox so the line is completely within
            // the bbox.

            PDRectangle borderBox = handleBorderBox(annotation, lineWidth);

            // lower left corner
            float x0 = borderBox.getLowerLeftX();
            float y0 = borderBox.getLowerLeftY();
            // upper right corner
            float x1 = borderBox.getUpperRightX();
            float y1 = borderBox.getUpperRightY();
            // mid points
            float xm = x0 + borderBox.getWidth() / 2;
            float ym = y0 + borderBox.getHeight() / 2;
            // see http://spencermortensen.com/articles/bezier-circle/
            // the below number was calculated from sampling content streams
            // generated using Adobe Reader
            float magic = 0.55555417f;
            // control point offsets
            float vOffset = borderBox.getHeight() / 2 * magic;
            float hOffset = borderBox.getWidth() / 2 * magic;

            contentStream.moveTo(xm, y1);
            contentStream.curveTo((xm + hOffset), y1, x1, (ym + vOffset), x1, ym);
            contentStream.curveTo(x1, (ym - vOffset), (xm + hOffset), y0, xm, y0);
            contentStream.curveTo((xm - hOffset), y0, x0, (ym - vOffset), x0, ym);
            contentStream.curveTo(x0, (ym + vOffset), (xm - hOffset), y1, xm, y1);
            contentStream.closePath();
        }

        contentStream.drawShape(lineWidth, hasStroke, hasBackground);
    }
    catch (IOException e)
    {
        LOG.error(e);
    }
    finally{
        IOUtils.closeQuietly(contentStream);
    }
}
 
Example 17
Source File: GenericSegment.java    From pdfxtk with Apache License 2.0 4 votes vote down vote up
public void setBoundingBox(PDRectangle bBox)
{
	x1 = bBox.getLowerLeftX(); x2 = bBox.getUpperRightX();
	y1 = bBox.getLowerLeftY(); y2 = bBox.getUpperRightY();
}