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

The following examples show how to use org.apache.pdfbox.pdmodel.common.PDRectangle#getLowerLeftY() . 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: 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 2
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 3
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 4
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 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.
 * </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 6
Source File: PDFVisibleTextStripper.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
public void processPage(PDPage page) throws IOException {
    PDRectangle pageSize = page.getCropBox();

    lowerLeftX = pageSize.getLowerLeftX();
    lowerLeftY = pageSize.getLowerLeftY();

    super.processPage(page);
}
 
Example 7
Source File: PdfToTextInfoConverter.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
public void processPage(PDPage page) throws IOException {
    PDRectangle pageSize = page.getCropBox();

    lowerLeftX = pageSize.getLowerLeftX();
    lowerLeftY = pageSize.getLowerLeftY();

    super.processPage(page);
}
 
Example 8
Source File: LayerUtility.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Places the given form over the existing content of the indicated page (like an overlay).
 * The form is enveloped in a marked content section to indicate that it's part of an
 * optional content group (OCG), here used as a layer. This optional group is returned and
 * can be enabled and disabled through methods on {@link PDOptionalContentProperties}.
 * <p>
 * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before calling this method to make
 * sure that the graphics state is reset.
 *
 * @param targetPage the target page
 * @param form the form to place
 * @param transform the transformation matrix that controls the placement of your form. You'll
 * need this if your page has a crop box different than the media box, or if these have negative
 * coordinates, or if you want to scale or adjust your form.
 * @param layerName the name for the layer/OCG to produce
 * @return the optional content group that was generated for the form usage
 * @throws IOException if an I/O error occurs
 */
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage,
        PDFormXObject form, AffineTransform transform,
        String layerName) throws IOException
{
    PDDocumentCatalog catalog = targetDoc.getDocumentCatalog();
    PDOptionalContentProperties ocprops = catalog.getOCProperties();
    if (ocprops == null)
    {
        ocprops = new PDOptionalContentProperties();
        catalog.setOCProperties(ocprops);
    }
    if (ocprops.hasGroup(layerName))
    {
        throw new IllegalArgumentException("Optional group (layer) already exists: " + layerName);
    }

    PDRectangle cropBox = targetPage.getCropBox();
    if ((cropBox.getLowerLeftX() < 0 || cropBox.getLowerLeftY() < 0) && transform.isIdentity())
    {
        // PDFBOX-4044 
        LOG.warn("Negative cropBox " + cropBox + 
                 " and identity transform may make your form invisible");
    }

    PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName);
    ocprops.addGroup(layer);

    PDPageContentStream contentStream = new PDPageContentStream(
            targetDoc, targetPage, AppendMode.APPEND, !DEBUG);
    contentStream.beginMarkedContent(COSName.OC, layer);
    contentStream.saveGraphicsState();
    contentStream.transform(new Matrix(transform));
    contentStream.drawForm(form);
    contentStream.restoreGraphicsState();
    contentStream.endMarkedContent();
    contentStream.close();

    return layer;
}
 
Example 9
Source File: PDAbstractAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get a rectangle enlarged by the differences.
 *
 * <p>
 * Creates a new rectangle with differences added to each side. If there are no valid
 * differences, then the original rectangle is returned.
 *
 * @param rectangle the rectangle.
 * @param differences the differences to apply.
 * @return the padded rectangle.
 */
PDRectangle addRectDifferences(PDRectangle rectangle, float[] differences)
{
    if (differences == null || differences.length != 4)
    {
        return rectangle;
    }
    
    return new PDRectangle(rectangle.getLowerLeftX() - differences[0],
            rectangle.getLowerLeftY() - differences[1],
            rectangle.getWidth() + differences[0] + differences[2],
            rectangle.getHeight() + differences[1] + differences[3]);
}
 
Example 10
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 11
Source File: PDAcroForm.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Check if there is a translation needed to place the annotations content.
 * 
 * @param appearanceStream
 * @return the need for a translation transformation.
 */
private boolean resolveNeedsTranslation(PDAppearanceStream appearanceStream)
{
    boolean needsTranslation = true;

    PDResources resources = appearanceStream.getResources();
    if (resources != null && resources.getXObjectNames().iterator().hasNext())
    {
        Iterator<COSName> xObjectNames = resources.getXObjectNames().iterator();

        while (xObjectNames.hasNext())
        {
            try
            {
                // if the BBox of the PDFormXObject does not start at 0,0
                // there is no need do translate as this is done by the BBox definition.
                PDXObject xObject = resources.getXObject(xObjectNames.next());
                if (xObject instanceof PDFormXObject)
                {
                    PDRectangle bbox = ((PDFormXObject)xObject).getBBox();
                    float llX = bbox.getLowerLeftX();
                    float llY = bbox.getLowerLeftY();
                    if (Float.compare(llX, 0) != 0 && Float.compare(llY, 0) != 0)
                    {
                        needsTranslation = false;
                    }
                }
            }
            catch (IOException e)
            {
                // we can safely ignore the exception here
                // as this might only cause a misplacement
            }
        }
        return needsTranslation;
    }
    
    return true;
}
 
Example 12
Source File: AppearanceGeneratorHelper.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Apply padding to a box.
 * 
 * @param box box
 * @return the padded box.
 */
private PDRectangle applyPadding(PDRectangle box, float padding)
{
    return new PDRectangle(box.getLowerLeftX() + padding, 
                           box.getLowerLeftY() + padding, 
                           box.getWidth() - 2 * padding,
                           box.getHeight() - 2 * padding);
}
 
Example 13
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 14
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 15
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 16
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 17
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 18
Source File: PDFPrintable.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will find the MediaBox with rotation applied, for this page by looking up the hierarchy
 * until it finds them.
 *
 * @return The MediaBox at this level in the hierarchy.
 */
static PDRectangle getRotatedMediaBox(PDPage page)
{
    PDRectangle mediaBox = page.getMediaBox();
    int rotationAngle = page.getRotation();
    if (rotationAngle == 90 || rotationAngle == 270)
    {
        return new PDRectangle(mediaBox.getLowerLeftY(), mediaBox.getLowerLeftX(),
                               mediaBox.getHeight(), mediaBox.getWidth());
    }
    else
    {
        return mediaBox;
    }
}
 
Example 19
Source File: RectanglesOverText.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/46080131/text-coordinates-when-stripping-from-pdfbox">
 * Text coordinates when stripping from PDFBox
 * </a>
 * <br/>
 * <a href="https://download-a.akamaihd.net/files/media_mwb/b7/mwb_I_201711.pdf">
 * mwb_I_201711.pdf
 * </a>
 * <p>
 * This test applies the OP's code to his example PDF file and indeed, there is an offset!
 * This is due to the <code>LegacyPDFStreamEngine</code> method <code>showGlyph</code>
 * which manipulates the text rendering matrix to make the lower left corner of the
 * crop box the origin. In the current version of this test, that offset is corrected,
 * see below. 
 * </p>
 */
@Test
public void testCoverTextByRectanglesMwbI201711() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("mwb_I_201711.pdf")  ) {
        PDDocument doc = Loader.loadPDF(resource);

        myStripper stripper = new myStripper();

        stripper.setStartPage(1); // fix it to first page just to test it
        stripper.setEndPage(1);
        stripper.getText(doc);

        TextLine line = stripper.lines.get(1); // the line i want to paint on

        float minx = -1;
        float maxx = -1;

        for (TextPosition pos: line.textPositions)
        {
            if (pos == null)
                continue;

            if (minx == -1 || pos.getTextMatrix().getTranslateX() < minx) {
                minx = pos.getTextMatrix().getTranslateX();
            }
            if (maxx == -1 || pos.getTextMatrix().getTranslateX() > maxx) {
                maxx = pos.getTextMatrix().getTranslateX();
            }
        }

        TextPosition firstPosition = line.textPositions.get(0);
        TextPosition lastPosition = line.textPositions.get(line.textPositions.size() - 1);

        // corrected x and y
        PDRectangle cropBox = doc.getPage(0).getCropBox();

        float x = minx + cropBox.getLowerLeftX();
        float y = firstPosition.getTextMatrix().getTranslateY() + cropBox.getLowerLeftY();
        float w = (maxx - minx) + lastPosition.getWidth();
        float h = lastPosition.getHeightDir();

        PDPageContentStream contentStream = new PDPageContentStream(doc, doc.getPage(0), PDPageContentStream.AppendMode.APPEND, false, true);

        contentStream.setNonStrokingColor(Color.RED);
        contentStream.addRect(x, y, w, h);
        contentStream.fill();
        contentStream.close();

        File fileout = new File(RESULT_FOLDER, "mwb_I_201711-withRectangles.pdf");
        doc.save(fileout);
        doc.close();
    }
}
 
Example 20
Source File: PDAbstractAppearanceHandler.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Get a padded rectangle.
 * 
 * <p>Creates a new rectangle with padding applied to each side.
 * .
 * @param rectangle the rectangle.
 * @param padding the padding to apply.
 * @return the padded rectangle.
 */
PDRectangle getPaddedRectangle(PDRectangle rectangle, float padding)
{
    return new PDRectangle(rectangle.getLowerLeftX() + padding, rectangle.getLowerLeftY() + padding,
            rectangle.getWidth() - 2 * padding, rectangle.getHeight() - 2 * padding);
}