Mori
Mori

Reputation: 2574

Convert Size.Width and Size.Height to millimeter

I have requirement to measure the text length in a PDF and wrap the line if the length exceeds a certain amount.

I already used the following code to determine length of the text (not sure it's working as expected but I need to solve another problem beforehand).

public static Size MeasureString(string s, Font font)
{
    SizeF result;
    using (var image = new Bitmap(1, 1))
    {
        using (var g = Graphics.FromImage(image))
        {
            result = g.MeasureString(s, font);
        }
    }
    return result.ToSize();
}

The return value of this Method is an instance of Size class. I am wondering how to convert Height and Width properties of this class to a human readable unit like millimeter.

Upvotes: 3

Views: 6272

Answers (1)

Christoph Fink
Christoph Fink

Reputation: 23103

To convert a Size, which is in pixels, to a "real life value" in milimeters you need one additional value:
dpi - dots per inch.

This should be defined by your PDF class. As soon as you have this value you can calculate the value you want:

const double milimetresPerInch = 25.4; // as one inch is 25.4 mm
double lengthInMilimeter = size.Width / dpi * milimetresPerInch; 

Upvotes: 2

Related Questions