View previous topic :: View next topic |
Author |
Message |
patches11
Joined: 24 Mar 2009 Posts: 3
|
Posted: Tue Mar 24, 2009 7:05 pm Post subject: Get width of a string? |
|
|
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 |
|
|
patches11
Joined: 24 Mar 2009 Posts: 3
|
Posted: Tue Mar 24, 2009 7:08 pm Post subject: |
|
|
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 |
|
|
patches11
Joined: 24 Mar 2009 Posts: 3
|
Posted: Tue Mar 24, 2009 8:51 pm Post subject: |
|
|
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 |
|
|
|