Calculating the height (in mm) of a text line in Microsoft Word RRS feed

  • Question

  • I'm making a program, in VB.NET, that creates and fills a table in a Microsoft Word document.

    Therefore I need a method to calculate the exact height, in millimeters, of a text line written with a certain font, at a certain size.

    Searching the Internet I've found this:

        Private Function GetFontHeight(ByVal FontName As String, _
                ByVal FontSize As Single) As Single
            Dim myGraphics As Graphics = Me.CreateGraphics()
            Dim myFont As Font = New Font(FontName, FontSize)
            myGraphics.PageUnit = GraphicsUnit.Millimeter
            Return myGraphics.MeasureString("Ag", myFont).Height * 0.9 '   <<< STRANGE!!!
        End Function

    The result was so imprecise that I had to add that 0.9 coefficient (I calculated it with several attempts by approximation). But the result is imprecise.

    There seems to be a discrepancy between the height of a line in the Graphics object, and the height of that same line in Microsoft Word. That discrepancy is about 10%. Strange, isn't it?

    Thursday, October 26, 2006 10:28 AM


  • Actually, that's expected :-) As a word processor, Word follows certain typographical conventions. One of these is that a certain amount of space is required around characters in order to produce a "readable page" (one the eye feels comfortable with). With proportional fonts, a certain amount of "kerning" (adjustment of horizontal space) is built in. Vertically, Word automatically adds roughly 10% of the font size to achieve the line spacing. But this measurement is, as you observe, rather imprecise.

    It is possible to set an EXACT line spacing; the option of part of the paragraph formatting. If this is so critical, it would probably be worthwhile to define that for the paragraphs in the table.

    Thursday, October 26, 2006 10:37 AM