I get some data from the web browser and should create a PDF document. I get the data looks like the following:
rectangle for the text placement;
font size
font name
text
The problem is I can't understand how I can calculate the text position on the PDF document correctly. The text in the browser looks like that
,
but the text in my Pdf looks like that
I get the X coord as I get it from the browser, but the text is shifted on the left. I read that the each glyph has a bounding box and placed the by the center.
I tried to calculate my x,y by the following way:
public Rectangle2D.Float calculatePosition(PDFont font, float fontSize, float x, float y, String text, float pageWidth, float pageHeight) {
final PDFontDescriptor fontDescriptor = font.getFontDescriptor();
final float ascent = fontDescriptor.getAscent();
final float descent = fontDescriptor.getDescent();
final float textHeight = (ascent - descent) * fontSize / 1000 + textRise;
final float textWidth = font.getStringWidth(text) * fontSize / 1000;
return new Rectangle2D.Float(x, y, textWidth, textHeight);
}
Can anyone help me, how to calculate x,y position?