PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index PDFsharp - moved to http://forum.pdfsharp.net/
Please visit the new PDFsharp forum at http://forum.pdfsharp.net/
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Get width of a string?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/
View previous topic :: View next topic  
Author Message
patches11



Joined: 24 Mar 2009
Posts: 3

PostPosted: Tue Mar 24, 2009 7:05 pm    Post subject: Get width of a string? Reply with quote

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.
Back to top
View user's profile Send private message
patches11



Joined: 24 Mar 2009
Posts: 3

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

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?
Back to top
View user's profile Send private message
patches11



Joined: 24 Mar 2009
Posts: 3

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

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);
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/ All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © phpBB Group. Hosted by phpBB.BizHat.com


Start Your Own YouTube Clone

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!
For Support, visit Forums.BizHat.com