This sample shows how to draw text starting at font's baseline.
PdfCanvas.DrawString methods draw text starting at the left-top corner of the text rectangle. To draw text starting at font's baseline, calculate the distance between the baseline and the top coordinate of the text rectangle.
The PdfFont.TopSideBearing property retrieves the distance between the baseline and the top of the font's bounding box. TopSideBearing
expressed in the glyph coordinate system. Use the value of the PdfFont.TransformationMatrix property to map TopSideBearing
to the canvas coordinate system.
For all PDF fonts except some tricky Type3 fonts, TransformationMatrix
has the following structure: { M11, 0, 0, M22, 0, 0 }
. For such fonts, map TopSideBearing
to the canvas coordinate system using this formula: font.TopSideBearing * font.TransformationMatrix.M22 * canvas.FontSize
.
With TopSideBearing
, expressed in the canvas coordinate system, you can draw text using a DrawString
method. Convert the baseline coordinate to the top coordinate of the text rectangle using this formula: top = (baselineY - topSideBearingCanvasSpace)
. Finally, use calculated top coordinate for a DrawString
call to draw text starting at the baseline position.