List of usage examples for org.apache.pdfbox.pdmodel.font PDFontDescriptor getDescent
public float getDescent()
From source file:com.planbase.pdf.layoutmanager.TextStyle.java
License:Apache License
private TextStyle(PDFont f, float sz, Color tc) { if (f == null) { throw new IllegalArgumentException("Font must not be null"); }/* www . j a va 2s . co m*/ if (tc == null) { tc = Color.BLACK; } font = f; textColor = tc; fontSize = sz; // Somewhere it says that font units are 1000 times page units, but my tests with // PDType1Font.HELVETICA and PDType1Font.HELVETICA_BOLD from size 5-200 show that 960x is // pretty darn good. // TODO: Fix font-size for other fonts. factor = fontSize / 960f; PDFontDescriptor fontDescriptor = font.getFontDescriptor(); float rawAscent = fontDescriptor.getAscent(); float rawDescent = fontDescriptor.getDescent(); // Characters look best with the descent size both above and below. Also acts as a good // default leading. ascent = rawAscent * factor; descent = rawDescent * -factor; leading = descent / 2; // height = ascent + descent + leading; float avgFontWidth = 500; try { avgFontWidth = font.getAverageFontWidth(); } catch (IOException ioe) { //throw new IllegalStateException("IOException probably means an issue reading font metrics from the underlying font file used in this PDF", ioe); ; // just use default if there's an exception. } avgCharWidth = avgFontWidth * fontSize; }
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;/*from w ww . j a va 2 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(); }