String title = "This is my wonderful title!"; // Or whatever title you want. int marginTop = 30; // Or whatever margin you want. PDDocument document = new PDDocument(); PDPage page = new PDPage() PDPageStreamContent stream = new PDPageContentStream(document, page); PDFont font = PDType1Font.HELVETICA_BOLD; // Or whatever font you want. int fontSize = 16; // Or whatever font size you want. float titleWidth = font.getStringWidth(title) / 1000 * fontSize; float titleHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText(); stream.setFont(font, fontSize); stream.moveTextPositionByAmount((page.getMediaBox().getWidth() - titleWidth) / 2, page.getMediaBox().getHeight - marginTop - titleheight); stream.drawString(title); stream.endText(); stream.close();
private static List<List<String>> getReAlignedTextBasedOnPageHeight(List<String> originalLines, PDFont font, float workablePageHeight) { final float newLineHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * FONT_SIZE + LINE_GAP; List<List<String>> realignedTexts = new ArrayList<>(); float consumedHeight = 0; List<String> linesInAPage = new ArrayList<>(); for (String line : originalLines) { if (newLineHeight + consumedHeight < workablePageHeight) { consumedHeight += newLineHeight; } else { consumedHeight = newLineHeight; realignedTexts.add(linesInAPage); linesInAPage = new ArrayList<>(); } linesInAPage.add(line); } realignedTexts.add(linesInAPage); return realignedTexts; }
protected String push(StringBuilder buffer, char character, TextPosition textPosition) { boolean bold = false; boolean italics = false; PDFontDescriptor descriptor = textPosition.getFont().getFontDescriptor(); if (descriptor != null) { bold = isBold(descriptor); italics = isItalic(descriptor); } buffer.append(bold ? open("b") : close("b")); buffer.append(italics ? open("i") : close("i")); appendEscaped(buffer, character); return buffer.toString(); }
PDFontDescriptor fd = getFontDescriptor(); if (fd != null)
float ascentAtFontSize = font.getFontDescriptor().getAscent() / FONTSCALE * fontSize; float baselineOffset = paddingEdge.getLowerLeftY() + (appearanceStream.getBBox().getHeight() - ascentAtFontSize)/2;
public Entry(String fontName, String usedName, PDFont font) { super(fontName); this.fontName = fontName; this.usedName = usedName; this.descriptor = font.getFontDescriptor(); this.baseFont = font; }
float height = (font.getFontDescriptor().getCapHeight() + -font.getFontDescriptor().getDescent()) * font.getFontMatrix().getScaleY(); if (height <= 0)
int fontSize = 14; PDFont font = PDType1Font.HELVETICA; font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize
float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * FONT_SIZE;
yTextPos = yTextPos - font.getFontDescriptor().getAscent() / FONTSCALE * fontSize;
private static float getAscent(PDFont font, float fontSize) { try { return (font.getFontDescriptor().getAscent() / 1000) * fontSize; } catch (Exception e) { } return 0.0f; }
private static float getDescent(PDFont font, float fontSize) { try { return (font.getFontDescriptor().getDescent() / 1000) * fontSize; } catch (Exception e) { } return 0.0f; }
PDFontDescriptor fontDescriptor = font.getFontDescriptor(); if (fontDescriptor != null)
public static float findMaxFontHeigth(PDFont font, int fontSize) { return (font.getFontDescriptor().getAscent() / 1000 * fontSize) - (font.getFontDescriptor().getDescent() / 1000 * fontSize); }
writeFont(font.getFontDescriptor(), name);
float fontCapAtSize = font.getFontDescriptor().getCapHeight() * fontScaleY; float fontDescentAtSize = font.getFontDescriptor().getDescent() * fontScaleY;
/** * @deprecated This method will be removed in a future release * @return current font width */ @Deprecated public float getFontWidth() { return font.getFontDescriptor().getFontBoundingBox().getWidth() / 1000 * fontSize; }
/** * <p> * Create basic {@link FontMetrics} for current font. * <p> * * @param font * The font from which calculation will be applied <<<<<<< HEAD * @throws IOException * If reading the font file fails ======= >>>>>>> using FreeSans * as default font and added new free fonts */ private static void createFontMetrics(final PDFont font) { final float base = font.getFontDescriptor().getXHeight() / 1000; final float ascent = font.getFontDescriptor().getAscent() / 1000 - base; final float descent = font.getFontDescriptor().getDescent() / 1000; fontMetrics.put(font.getName(), new FontMetrics(base + ascent - descent, ascent, descent)); }
protected String push(StringBuilder buffer, char character, TextPosition textPosition) { boolean bold = false; boolean italics = false; PDFontDescriptor descriptor = textPosition.getFont().getFontDescriptor(); if (descriptor != null) { bold = isBold(descriptor); italics = isItalic(descriptor); } buffer.append(bold ? open("b") : close("b")); buffer.append(italics ? open("i") : close("i")); appendEscaped(buffer, character); return buffer.toString(); }
private void drawUnderline(String text, Color color) { if (underline != Font.U_NONE && !StringUtils.isEmpty(text)) { int fontSize = (int) stream.getFontSize(); PDFont font = stream.getFont(); float descend = font.getFontDescriptor().getDescent() / 1000 * fontSize; drawUnderline(text,color, descend); if (underline == Font.U_DOUBLE || underline == Font.U_DOUBLE_ACCOUNTING) { drawUnderline(text,color, descend / 2); } } }