public Rectangle getTextSize ( String text , Font font ) { template . saveState ( ) ; // get the font DefaultFontMapper mapper = new DefaultFontMapper ( ) ; BaseFont bf = mapper . awtToPdf ( font ) ; template . setFontAndSize ( bf , font . getSize ( ) ) ; // calculate text width and height float textWidth = template . getEffectiveStringWidth ( text , false ) ; float ascent = bf . getAscentPoint ( text , font . getSize ( ) ) ; float descent = bf . getDescentPoint ( text , font . getSize ( ) ) ; float textHeight = ascent - descent ; template . restoreState ( ) ; return new Rectangle ( 0 , 0 , textWidth , textHeight ) ; }
Return the text box for the specified text and font .