Docotic.Pdf Library Help > Samples > Text > Draw text from baseline

This sample shows how to draw text starting at font's baseline.

PdfCanvas.DrawString method draws string starting at 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.

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 PdfFont.TransformationMatrix 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 by this formula: font.TopSideBearing * font.TransformationMatrix.M22 * canvas.FontSize.

With TopSideBearing, expressed in the canvas coordinate system, you can draw text using DrawString method. Convert baseline coordinate to the top coordinate of the text rectangle by this formula: top = (baselineY - topSideBearingCanvasSpace). Finally, pass calculated top coordinate to DrawString method to draw text starting at the baseline position.

C#
using System.Diagnostics;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class DrawTextFromBaseline
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "DrawTextFromBaseline.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                PdfCanvas canvas = pdf.Pages[0].Canvas;
                canvas.FontSize = 30;
                canvas.Font = pdf.AddFont(PdfBuiltInFont.TimesItalic);

                // draw straight line across the page
                const double baselinePosition = 100;
                canvas.CurrentPosition = new PdfPoint(0, baselinePosition);
                canvas.DrawLineTo(new PdfPoint(pdf.Pages[0].Width, baselinePosition));

                // calculate distance from the baseline to the top of the font bounding box
                double distanceToBaseLine = getDistanceToBaseline(canvas.Font, canvas.FontSize);

                // draw text so that baseline of the text placed exactly over the straight line
                canvas.DrawString(10, baselinePosition - distanceToBaseLine, "gyQW");

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }

        private static double getDistanceToBaseline(PdfFont font, double fontSize)
        {
            return font.TopSideBearing * font.TransformationMatrix.M22 * fontSize;
        }
    }
}
VB
Imports System.Diagnostics

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class DrawTextFromBaseline
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "DrawTextFromBaseline.pdf"

            Using pdf As New PdfDocument()
                Dim canvas As PdfCanvas = pdf.Pages(0).Canvas
                canvas.FontSize = 30
                canvas.Font = pdf.AddFont(PdfBuiltInFont.TimesItalic)

                ' draw straight line across the page
                Const baselinePosition As Double = 100
                canvas.CurrentPosition = New PdfPoint(0.0, baselinePosition)
                canvas.DrawLineTo(New PdfPoint(pdf.Pages(0).Width, baselinePosition))

                ' calculate distance from the baseline to the top of the font bounding box
                Dim distanceToBaseLine As Double = getDistanceToBaseline(canvas.Font, canvas.FontSize)

                ' draw text so that baseline of the text placed exactly over the straight line
                canvas.DrawString(10, baselinePosition - distanceToBaseLine, "gyQW")

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub

        Private Shared Function getDistanceToBaseline(font As PdfFont, fontSize As Double) As Double
            Return font.TopSideBearing * font.TransformationMatrix.M22 * fontSize
        End Function
    End Class
End Namespace