PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Fri Apr 25, 2025 1:26 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Mar 25, 2025 11:48 am 
Offline

Joined: Thu Mar 31, 2022 8:49 am
Posts: 2
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:
Image

Is there any way to get the actual text dimensions?


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 27, 2025 11:48 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3128
Location: Cologne, Germany
Mithrandir wrote:
Is there any way to get the actual text dimensions?

You already have them.
Things can be simple if you keep your code simple.
You can invoke DrawString with an XRect, eliminating the need for offset calculations.

The result of "font.GetHeight()" can be larger than the height returned by "gfx.MeasureString".
You use the potentially smaller value as height of the rectangle, but use the potentially larger value as the offset for the text. As a result, you move the text too far.
If you want to stick to using DrawString with a point and an offset, use identical values for rectangle height and offset or ensure that offset is not larger than rectangle height.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Sun Apr 20, 2025 7:49 am 
Offline

Joined: Thu Mar 31, 2022 8:49 am
Posts: 2
Thank you for the advice!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 23 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group