We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Text width method ?

Hi,

Do DocIO have a fucntion to get the width (in pixels) of a text range depending the font used ? (like the "MeasureString" method of a graphics object in GDI+).

Thank's for your answer.

Best Regards.

Chris

5 Replies

SV Sarathkumar V Syncfusion Team March 31, 2016 12:43 PM UTC

Hi Mouries,

Thank you for using Syncfusion products.

Currently DocIO does not have API to get the width(in pixel) of a text range depending on the font used. As a workaround, you can use the Graphics.MeasureString() method in your application to measure the text range width. For your references, please refer the below code snippet which measures the text range depending on font using Graphics.MeasureString() method at sample level and let us know if it helps.

Code Snippet:

            WordDocument document = new WordDocument();

            document.EnsureMinimal();

            WParagraph paragraph = document.LastParagraph;

            WTextRange range = paragraph.AppendText("Hello World!") as WTextRange;

            range.CharacterFormat.FontName = "Arial";

            range.CharacterFormat.FontSize = 15;

            range.CharacterFormat.Bold = true;

            //Create new bitmap image

            Bitmap bmp = new Bitmap(1, 1);

            //Create new Graphics instance from bitmap image

            Graphics graphic = Graphics.FromImage(bmp);

            //set the image resolution

            bmp.SetResolution(120, 120);

            //Set the page unit as pixel to get the measured values in pixel unit

            graphic.PageUnit = GraphicsUnit.Pixel;

            //Measure the text range text value

            float width = graphic.MeasureString(range.Text, range.CharacterFormat.Font).Width;

            graphic.Dispose();

            bmp.Dispose();
            document.Close();

For more details, please refer the following MSDN link:

https://msdn.microsoft.com/en-us/library/6xe5hazb(v=vs.110).aspx

Regards,
Sarath



MO Mouries April 1, 2016 05:06 PM UTC

Hi,

Thank you for your answer.
Unfortunatly, your solution doesn't work because values returned by graphices measure is never the same than DocIO cell width.

Here is my test to explain the problem :

- I create from scratch a word docuent with Microisoft Woird.
- I insert a table, 1 row, 2 cells, no borders, no margins, no paddings
- In the firsty cell, I put this text : "Test string in cell"
- I resize manually the celll width to fit exactly the texte.
- I inspect the cell properties and Word tells me the cell is 2.5 cm width

I start a test code in Visual studio.
- First, I ask with the PointsConverter function the wiidth of 2.5 cm : answer is 70.86 px
- Second : I open and browse my word document with DocIO, and inspect the first cell width of the table : answer is 70.85 px. We are OK.
- Finally, I use graphics object like suggested to measure my string with the same foint : answer is 75.17 px. Higher.... Why ???

Bellow is my test code you can use and the word doc used is included in this message.

Thank you for any idea to explain why DocIO cell width is not the same than gtaphics object.

Regards.

Chris




REM Word doc cell was manually resize with 2.5 cm

        REM From DocID convresion

        MsgBox(PointsConverter.FromCm(2.5))

 

        REM Open Word Doc with DOCIO and contraol the tablecell width

        REM At this point, we are OK with the value returned by PointsConverter

        Dim MyDoc = New WordDocument("C:\Data\FT.Doc")

        Dim MyTable As WTable = MyDoc.Sections(0).Tables(0)

 

        MsgBox(MyTable.Rows(0).Cells(0).Paragraphs(0).Text)

        MsgBox(MyTable.Rows(0).Cells(0).Width)

 

        REM From Graphics object, obtain text width measure

        REM Wer are NOT Ok with the value return by the DocIO cell with (the value is always higher)

        Dim MyImage As Bitmap = New Bitmap(1, 1)

        Dim MyGraphic As Drawing.Graphics = Drawing.Graphics.FromImage(MyImage)

 

        MyImage.SetResolution(120, 120)

        MyGraphic.PageUnit = GraphicsUnit.Point

 

        MsgBox(MyGraphic.MeasureString(MyTable.Rows(0).Cells(0).Paragraphs(0).Text, New Font("Calibri", 10, FontStyle.Bold)).Width)

        MyGraphic.Dispose()

        MyImage.Dispose()



Attachment: FT_71be12f5.zip


SV Sarathkumar V Syncfusion Team April 4, 2016 12:23 PM UTC

Hi Mouries,

Thanks for your update.

On further analyzing the given Word document and code snippet, we have found that you have resized the cell width to 2.5 cm(70.85 pt) to fit the text “Test string in cell” but it is still possible to resize the cell width to 2.42 cm (68.5 pt) to fit the text exactly equal to its width. Also we have found that you have used font Calibiri with size 10 and also you have used the font style as bold but actual input document does not contain the bold style. Graphics.MeasureString(String, Font) 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. For your reference, please refer the below MSDN link to know the more about Graphics.MeasureString() method :

https://msdn.microsoft.com/en-us/library/6xe5hazb(v=vs.110).aspx#Anchor_2

https://msdn.microsoft.com/en-us/library/system.drawing.graphics.measurestring(v=vs.110).aspx

This causes the width difference between the Microsoft Word’s table cell width and Graphics.MeasureString() method measured text width.

Kindly refer the following modified document which has the table cell width exactly equal to text width and code snippet to measures the text range depending on font using Graphics.MeasureString() method at sample level and let us know if it helps.

Modified Document link:
http://www.syncfusion.com/downloads/support/forum/123559/ze/FT467124706

Modified code snippet:

        REM Word doc cell was manually resize with 2.42 cm

        REM From DocID convresion

        MsgBox(PointsConverter.FromCm(2.42))

        REM Open Word Doc with DOCIO and contraol the tablecell width

        REM At this point, we are OK with the value returned by PointsConverter

        Dim MyDoc = New WordDocument("FT.doc")

        Dim MyTable As WTable = MyDoc.Sections(0).Tables(0)

        MsgBox(MyTable.Rows(0).Cells(0).Paragraphs(0).Text)

        MsgBox(MyTable.Rows(0).Cells(0).Width)

        REM From Graphics object, obtain text width measure

        REM Wer are NOT Ok with the value return by the DocIO cell with (the value is always higher)

        Dim MyImage As Bitmap = New Bitmap(1, 1)

        Dim MyGraphic As Drawing.Graphics = Drawing.Graphics.FromImage(MyImage)

        MyImage.SetResolution(120, 120)

        MyGraphic.PageUnit = GraphicsUnit.Point

        Dim stringformat As New StringFormat(StringFormat.GenericTypographic)

        stringformat.FormatFlags = stringformat.FormatFlags And Not StringFormatFlags.LineLimit

        stringformat.FormatFlags = stringformat.FormatFlags Or StringFormatFlags.MeasureTrailingSpaces

        stringformat.FormatFlags = stringformat.FormatFlags Or StringFormatFlags.NoClip

        MsgBox(MyGraphic.MeasureString(MyTable.Rows(0).Cells(0).Paragraphs(0).Text, New Font("Calibri", 10, FontStyle.Regular), New Point(0, 0), stringformat).Width)

        MyGraphic.Dispose()
        MyImage.Dispose()

Regards,
Sarath



MO Mouries April 4, 2016 02:13 PM UTC

Hi Sarath,

Well done ! It works perfectly now !
Thanks a lot for your assitance.

Best regards.

Chris


SV Sarathkumar V Syncfusion Team April 5, 2016 04:18 AM UTC

Hi Mouries,

Thanks for your update.

We are glad to know that the provided solution has satisfied your requirement. Please let us know if you need any further assistance, we will be happy to assist you as always.

Regards,
Sarath

Loader.
Up arrow icon