Java Code Examples for org.apache.fontbox.util.BoundingBox#getHeight()

The following examples show how to use org.apache.fontbox.util.BoundingBox#getHeight() . 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: PDFExtractor.java    From inception with Apache License 2.0 5 votes vote down vote up
private Shape calculateFontBounds(TextPosition text) throws IOException
{
    // glyph space -> user space
    // note: text.getTextMatrix() is *not* the Text Matrix, it's the Text Rendering Matrix
    AffineTransform at = text.getTextMatrix().createAffineTransform();

    // show rectangle with the real vertical bounds, based on the font bounding box y values
    // usually, the height is identical to what you see when marking text in Adobe Reader
    PDFont font = text.getFont();
    BoundingBox bbox = font.getBoundingBox();

    // advance width, bbox height (glyph space)
    float xadvance = font
        .getWidth(text.getCharacterCodes()[0]); // todo: should iterate all chars
    Rectangle2D.Float rect = new Rectangle2D.Float(0, bbox.getLowerLeftY(), xadvance,
        bbox.getHeight());

    if (font instanceof PDType3Font) {
        // bbox and font matrix are unscaled
        at.concatenate(font.getFontMatrix().createAffineTransform());
    }
    else {
        // bbox and font matrix are already scaled to 1000
        at.scale(1 / 1000f, 1 / 1000f);
    }
    Shape s = at.createTransformedShape(rect);
    s = flipAT.createTransformedShape(s);
    s = rotateAT.createTransformedShape(s);
    return s;
}
 
Example 2
Source File: PdfRenderer.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
    text = text.toLowerCase();
    int index = text.indexOf(mTextToHighlight);
    if (index != -1) {
        PDPage          currentPage     = getCurrentPage();
        PDRectangle     pageBoundingBox = currentPage.getBBox();
        AffineTransform flip            = new AffineTransform();
        flip.translate(0, pageBoundingBox.getHeight());
        flip.scale(1, -1);
        PDRectangle mediaBox    = currentPage.getMediaBox();
        float       mediaHeight = mediaBox.getHeight();
        float       mediaWidth  = mediaBox.getWidth();
        int         size        = textPositions.size();
        while (index != -1) {
            int last = index + mTextToHighlight.length() - 1;
            for (int i = index; i <= last; i++) {
                TextPosition      pos  = textPositions.get(i);
                PDFont            font = pos.getFont();
                BoundingBox       bbox = font.getBoundingBox();
                Rectangle2D.Float rect = new Rectangle2D.Float(0, bbox.getLowerLeftY(), font.getWidth(pos.getCharacterCodes()[0]), bbox.getHeight());
                AffineTransform   at   = pos.getTextMatrix().createAffineTransform();
                if (font instanceof PDType3Font) {
                    at.concatenate(font.getFontMatrix().createAffineTransform());
                } else {
                    at.scale(1 / 1000.0f, 1 / 1000.0f);
                }
                Shape           shape     = flip.createTransformedShape(at.createTransformedShape(rect));
                AffineTransform transform = mGC.getTransform();
                int             rotation  = currentPage.getRotation();
                if (rotation != 0) {
                    switch (rotation) {
                    case 90:
                        mGC.translate(mediaHeight, 0);
                        break;
                    case 270:
                        mGC.translate(0, mediaWidth);
                        break;
                    case 180:
                        mGC.translate(mediaWidth, mediaHeight);
                        break;
                    default:
                        break;
                    }
                    mGC.rotate(Math.toRadians(rotation));
                }
                mGC.fill(shape);
                if (rotation != 0) {
                    mGC.setTransform(transform);
                }
            }
            index = last < size - 1 ? text.indexOf(mTextToHighlight, last + 1) : -1;
        }
    }
}