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
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()
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