I know this is an old question, but a quazi solution for it. I have tested my solution a few times and it seems to be accurate but not precise. The basis of this code is directly from an MSDN example and this answer on SO, the only thing I am doing differently is changing the units from points to inches.
MSDN Graphics.MeasureString()
private float GetTextWidth(string fontFace, float fontSize, string text)
{
SizeF size = new SizeF();
Font font = new Font(fontFace, fontSize);
//Using a Bitmap object for frame of reference in order to get to a Graphics object
using (Bitmap b = new Bitmap(1, 1))
{
//Graphics object allows string measurement
using (Graphics g = Graphics.FromImage(b))
{
//change the units to inches
g.PageUnit = GraphicsUnit.Inch;
//Perform the measurement
size = g.MeasureString(text, font);
}
}
//Print to console if you want
//Console.WriteLine(size.Width);
return size.Width;
}
I tested this out with the following inputs:
("Arial", 10, "Page 1 of 1") and I got 0.777972 inches
If you read the Remarks of the MSDN link above you will see that there is a little extra space padded onto that figure. You have to be aware that this happens:
The MeasureString method is designed for use with individual strings
and includes a small amount of extra space before and after the string
to allow for overhanging glyphs.
When I measured this on a piece of paper end to end I got about 0.625, that is a difference of 0.152972 which is essentially ~20% padding. I'm guessing it is distributed on either side evenly.
Also be careful with which Fonts you use - for example Helvetica is not a supported font by default, therefore the Font object will default to Arial if it can't find your font.