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();
/** * Sets the font service provider. */ public synchronized void setProvider(FontProvider fontProvider) { fontInfoByName = createFontInfoByName(fontProvider.getFontInfo()); this.fontProvider = fontProvider; }
/** * Returns the value of the symbolic flag, allowing for the fact that the result may be * indeterminate. */ protected final Boolean getSymbolicFlag() { if (getFontDescriptor() != null) { // fixme: isSymbolic() defaults to false if the flag is missing so we can't trust this return getFontDescriptor().isSymbolic(); } return null; }
spaceIndex = text.length(); String subString = text.substring(0, spaceIndex); float size = fontSize * pdfFont.getStringWidth(subString) / 1000; System.out.printf("'%s' - %f of %f\n", subString, size, width); if (size > width)
@Override public Vector getDisplacement(int code) throws IOException { if (isVertical()) { return new Vector(0, descendantFont.getVerticalDisplacementVectorY(code) / 1000f); } else { return super.getDisplacement(code); } }
/** * Constructor. * * @param fontDictionary The font dictionary according to the PDF specification. */ PDCIDFont(COSDictionary fontDictionary, PDType0Font parent) throws IOException { this.dict = fontDictionary; this.parent = parent; readWidths(); readVerticalDisplacements(); }
@Override public PDResources getResources() { return font.getResources(); }
@Override public PDFontDescriptor getFontDescriptor() { return descendantFont.getFontDescriptor(); }
@Override public String getName() { return getBaseFont(); }
/** * Returns the CID for the given character code. If not found then CID 0 is returned. * * @param code character code * @return CID */ public int codeToCID(int code) { return descendantFont.codeToCID(code); }
@Override public BoundingBox getBoundingBox() throws IOException { if (fontBBox == null) { fontBBox = generateBoundingBox(); } return fontBBox; }
/** * A convenience method that sets the flag bit. * * @param flag The flag value. */ public void setNonSymbolic( boolean flag ) { setFlagBit( FLAG_NON_SYMBOLIC, flag ); }
/** * A convenience method that checks the flag bit. * * @return The flag value. */ public boolean isItalic() { return isFlagBitOn( FLAG_ITALIC ); }
@Override public GeneralPath getNormalizedPath(int code) throws IOException { return getPath(code); }
@Override public float getAverageFontWidth() { if (avgWidth == null) { avgWidth = getAverageCharacterWidth(); } return avgWidth; }
@Override public Matrix getFontMatrix() { return descendantFont.getFontMatrix(); }
@Override public float getHeight(int code) throws IOException { return descendantFont.getHeight(code); }
@Override protected byte[] encode(int unicode) throws IOException { return descendantFont.encode(unicode); }
@Override public boolean isDamaged() { return descendantFont.isDamaged(); }
spaceIndex = text.length(); String subString = text.substring(0, spaceIndex); float size = fontSize * pdfFont.getStringWidth(subString) / 1000; System.out.printf("'%s' - %f of %f\n", subString, size, width); if (size > width)