Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I wonder if anyone can help me with a small problem...

 

I am currently trying to output RTF files from VB.NET and I have become stuck on one thing. I am trying to work out how many lines of text I can fit onto a page based on the page size, the size of the top and bottom margins and the font size.

 

To make sure that I could work it out right, I opened an empty Word document. The top and bottom margins were set to 2.54 cm and the paper height was 29.7cm (A4). That means that I have a working area of 24.62cm.

 

The font size that I am working with is 24points. I converted this to centimeters using an online convcerter and it gave me 0.846666666666667cm.

 

Here is the strange part: when I manually tested it in Word, I could fit 25 lines of text down the page, but if you divide the working area of the page (24.62cm) by the font size, it gives you 29.078!! I made sure that there was no extra line spacing in Word, just in case that was the problem.

 

Can anyone help me with this? If I don't know how to calculate how many lines I can fit on a page, I will not be able to finish developing the report generator that I am working on.

 

Many thanks in advance

 

Jamie

 

http://www.angelfire.com/az/deaflab/measure.html

  • *Experts*
Posted

Although I can't help you exactly, I can mention that there is also a bit of room between lines for readability. You couldn't want one font directly on top of another (not usually). I have no idea how that little extra margin is calculated though. Consider it similar to a double spaced document. The single spaced document still has a little extra room between lines.

 

I wonder if the MeasureString method of the Graphics object (I think) can calculate heights? I seem to recall it returning a Rect structure, which would imply it does. You could try that instead of figuring out the height of one line.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted (edited)

Give this a try:

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim count As Integer = 0
Dim linesPerPage As Single = 0
Dim printFont As Font = New Font("Arial", 14, FontStyle.Bold)
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)

Then do this:

While count < linesPerPage - 10
'loop stuff to be printed in here
count += 1
If count > linesPerPage Then
           e.HasMorePages = True
       Else
           e.HasMorePages = False
       End If
End While

 

Jon

Edited by jfackler

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...