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

E

euthymos

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?
 
J

Jezebel

The size of a font (eg 12pt or whatever) is the distance from the top of the
highest ascender to the bottom of the lowest descender. The actual line
height is this distance plus whatever linespacing is added by virtue of the
paragraph linespacing value. The default "auto" is normally 20 percent.

But why do you need to know this to populate your table?
 
J

Jonathan West

Create 2 lines of text in the same font, and use the
Information(wdVerticalPositionRelativeToPage) property to get the distance
of each line (in points) from the top of the page. Subtract one from the
other to get the line height, and use the PointsToMillimeters function to
convert to mm.

That will get you the line spacing for the font *as implemented in Word*,
which I presume is what you are looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top