Example usage for org.apache.pdfbox.pdmodel.font PDFontDescriptor getFontBoundingBox

List of usage examples for org.apache.pdfbox.pdmodel.font PDFontDescriptor getFontBoundingBox

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.font PDFontDescriptor getFontBoundingBox.

Prototype

public PDRectangle getFontBoundingBox() 

Source Link

Document

This will get the fonts bounding box.

Usage

From source file:com.helger.pdflayout.spec.PDFFont.java

License:Apache License

public PDFFont(@Nonnull final PDFont aFont) {
    m_aFont = ValueEnforcer.notNull(aFont, "Font");
    final PDFontDescriptor aFD = aFont.getFontDescriptor();
    // 2.0.0 code. Does not work with 1.8.4
    // if (aFD == null)
    // {/*from  w ww . ja va  2  s .  c  o m*/
    // if (aFont instanceof PDType0Font)
    // {
    // final PDFont aDescendantFont = ((PDType0Font) aFont).getDescendantFont
    // ();
    // if (aDescendantFont != null)
    // aFD = aDescendantFont.getFontDescriptor ();
    // }
    // }
    if (aFD == null)
        throw new IllegalArgumentException("Failed to determined FontDescriptor from specified font " + aFont);

    m_fBBHeight = aFD.getFontBoundingBox().getHeight();
}

From source file:de.hrogge.CompactPDFExport.PDFSeite.java

License:Apache License

public float berechneTextUeberlauf(PDFont font, int x1, int x2, int height, String text) throws IOException {
    PDFontDescriptor descr = font.getFontDescriptor();
    float boxProp, textProp;
    float boxWidth, boxHeight;
    float textWidth, textHeight;

    boxHeight = (pageHeight / 60.0f * height) - 2 * randText;
    boxWidth = (getX(x2) - getX(x1)) - 2 * randText;
    boxProp = boxWidth / boxHeight;//from   w ww  . ja va2 s  . c o  m

    textHeight = (descr.getFontBoundingBox().getHeight()) / 1000f;
    textWidth = font.getStringWidth(text) / 1000f;
    textProp = textWidth / textHeight;

    if (textProp > boxProp) {
        return textProp / boxProp;
    }
    return 1.0f;
}

From source file:de.hrogge.CompactPDFExport.PDFSeite.java

License:Apache License

public void drawText(PDFont font, int x1, int x2, int y1, int y2, String text, boolean center)
        throws IOException {
    PDFontDescriptor descr = font.getFontDescriptor();
    float boxProp, textProp;
    float boxWidth, boxHeight;
    float textWidth, textHeight;
    float shiftX, shiftY;

    boxHeight = (getY(y1) - getY(y2)) - 2 * randText;
    boxWidth = (getX(x2) - getX(x1)) - 2 * randText;
    boxProp = boxWidth / boxHeight;/*  w  w w.j a  v  a2  s  .c o m*/

    textHeight = (descr.getFontBoundingBox().getHeight()) / 1000f;
    textWidth = font.getStringWidth(text) / 1000f;
    textProp = textWidth / textHeight;

    /* begrenze vertikale Skalierung */
    while (textProp / boxProp > 1.75 && text.contains(" ")) {
        text = text.substring(0, text.lastIndexOf(' ')) + "...";
        textWidth = font.getStringWidth(text) / 1000f;
        textProp = textWidth / textHeight;
    }

    stream.beginText();

    if (textProp > boxProp) {
        /* scale over width */
        stream.setFont(font, boxWidth / textWidth);

        shiftX = 0f;
        shiftY = boxHeight / 2 - (textHeight * boxWidth / textWidth) / 2;
        shiftY += -descr.getDescent() / 1000 * (boxWidth / textWidth);
    } else {
        /* scale over height */
        stream.setFont(font, boxHeight / textHeight);

        if (center) {
            shiftX = boxWidth / 2 - textWidth * (boxHeight / textHeight) / 2;
        } else {
            shiftX = 0f;
        }
        shiftY = -descr.getDescent() / 1000 * (boxHeight / textHeight);
    }

    stream.moveTextPositionByAmount(getX(x1) + randText + shiftX, getY(y2) + randText + shiftY);
    stream.drawString(text);
    stream.endText();
}