PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 10:01 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Get width of a string?
PostPosted: Tue Mar 24, 2009 7:05 pm 
Offline

Joined: Tue Mar 24, 2009 5:55 pm
Posts: 3
I'm trying to figure out how to get the width of a string in a paragraph so that I can set the size of a table column to fit the text. I have this so far:

Code:
XFont f = new XFont(FontFamily.GenericSansSerif.GetName(0), 1);
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
//g.PageUnit = GraphicsUnit.Inch; //this has no effect for some reason
XGraphics xg = XGraphics.FromGraphics(g, new XSize(100, 100), XGraphicsUnit.Inch);
XSize size = xg.MeasureString("(30 days ago)", f);

But I have no idea what the units are in, and it isn't linked to the font and size used in the table cell's paragraph.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Mar 24, 2009 7:08 pm 
Offline

Joined: Tue Mar 24, 2009 5:55 pm
Posts: 3
I just saw the post on centering text, and figured it might need to know the size of the text to accomplish this feat. Sure enough, it does:

Code:
// Get an XGraphics object for drawing beneath the existing content
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
 
// Get the size (in point) of the text
XSize size = gfx.MeasureString(watermark, font);

But, I don't know how to get a PdfPage object from a MigraDoc document. Any pointers?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Mar 24, 2009 8:51 pm 
Offline

Joined: Tue Mar 24, 2009 5:55 pm
Posts: 3
Figured it out:
Code:
Style style = table.Document.Styles["Normal"];
MigraDoc.DocumentObjectModel.TextMeasurement tm = new TextMeasurement(style.Font);
SizeF size = tm.MeasureString("(30 days ago)", UnitType.Inch);


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 433 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