Quantcast
Channel: .NET Framework Class Libraries forum
Viewing full articles (8156 articles)
Browse article index
↧

System.Drawing.Graphics.MeasureString() does not return a exact width for "Sakkal Majalla" font

$
0
0

Hi All,

In our requirement, we are converting a Microsoft Word to PDF with in our library. To achieve this requirement, we measure a string with required font by using  System.Drawing.Graphics.MeasureString(). Here, when we try to measure the text "الرقم" with "Sakkal Majalla" font, it does not return the exact width as per MS Word does.

Please find the below code snippet that we are using to measure the text,

            //Create a bitmap
            Bitmap bmp = new Bitmap(1, 1);

            //Set the resolution of the bitmap as 120, 120
            bmp.SetResolution(120, 120);

            //Get the graphics from bitmap
            Graphics _graphics = Graphics.FromImage(bmp);
            //Set graphics page unit as point
            _graphics.PageUnit = GraphicsUnit.Point;

            //String to measure
            string text = "الرقم";

            //String format to measure
            StringFormat stringFormat = new StringFormat();
            stringFormat.FormatFlags = StringFormatFlags.FitBlackBox;
            stringFormat.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
            stringFormat.FormatFlags |= StringFormatFlags.NoClip;
            stringFormat.Trimming = StringTrimming.Word;

            FontStyle fontStyle = FontStyle.Bold;

            //Create System.Drawing.Font to measure
            Font font = new Font("Sakkal Majalla", 14, fontStyle);

            //Measure the string with GDI
            SizeF size = _graphics.MeasureString(text, font, new PointF(0, 0), stringFormat);

            //Return a width of the text as 27.890625
            float width = size.Width;

By using this measuring code, it return a width of the text as "27.890625" point but when we measure the same text with MS Word it's fit the specified text with in "21.95" point. 

How we measure the text in MS Word?

We are measuring a text in MS Word by fit the text with in the simple table cell with required font. After fitting that text in a cell, we consider that width of the cell as text width.

Could you please suggest us a solution to get the same width as in MS Word via Graphics.MeasureString().

Thanks,

Ramaraj Marimuthu


↧

Viewing full articles (8156 articles)
Browse article index

Latest Images

Trending Articles



Latest Images