Hi everyone,
I'm using PdfSharp-gdi on Windows.
I'm trying to align the upper edges of two pieces of text. Unfortunately, MeasureString does not return the actual size of the text.
This sample code writes text and then puts a rectangle around the measured size:
Code:
PdfDocument document = new PdfDocument();
document.Info.Title = "String Measurement Example";
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
string text1 = "Hello";
string text2 = "Apple";
XFont font = new XFont("Arial", 12);
XSize size1 = gfx.MeasureString(text1, font);
XSize size2 = gfx.MeasureString(text2, font);
XRect rect1 = new XRect(50, 100, size1.Width, size1.Height);
XRect rect2 = new XRect(50, 200, size2.Width, size2.Height);
XPen pen = new XPen(XColors.Black, 0.5);
gfx.DrawRectangle(pen, rect1);
gfx.DrawRectangle(pen, rect2);
gfx.DrawString(text1, font, XBrushes.Black, rect1.X, rect1.Y + font.GetHeight());
gfx.DrawString(text2, font, XBrushes.Black, rect2.X, rect2.Y + font.GetHeight());
string filename = "StringMeasurementExample.pdf";
document.Save(filename);
Result:

Is there any way to get the actual text dimensions?