At the end of this note is a VB .NET sample of printing a text file. MS
provided it in .NET help. It shows how to print lines of text all in the
same font. The PrintPage event handler calculates the number of lines of
text that can be printed as follows:
linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics)
Well, this cant be right, can it? With an old HP LaserJet 6P, with no
margins, MarginBounds.Height is 1100, or 11 inches. You lose 1/6 inch top
and bottom as nonprinting area, for a total of 1/3 inch.
e.Graphics.ClipBounds.Height is 1066.666, or 11 inches less 1/3 an inch, so
it would seem that the lines per page calculation would need to take this
into account. The code provided by MS in the sample yields linesperpage that
is 2 or 3 lines too big.
I have tried many ways to compute lines per page, and nothing seems to work
right in all cases. My objective is to get my VB program to know exactly how
many lines it can print to a page under Courier New font sizes 8, 10, and 12
points for portrait and landscape. I can't figure it out - sometimes there
is either a lost line or extra white space at the bottom of a page. By this
I mean if I code a calculation and run the 6 cases (portrait/landscape cross
8/10/12 points), the six answers I get are some mix of one too low, correct,
and one too high.
Subsequently, I have tried using
If Not e.Graphics.IsVisible(rf.X, rf.Y, 1, rf.Height) Then Exit While
In other words, I print lines until the IsVisible function indicates not
visible, and end the while loop this way rather than by counting lines. This
shows some promise, but I have more testing to do.
So, how are you supposed to do this simple thing? My printing requirement
is for a very dense one page work sheet in a monospace font, and this means I
have to accurately know how many lines I can actually print.
Example Basic code follows:
Dim fileToPrint As System.IO.StreamReader
Dim printFont As System.Drawing.Font
Private Sub PrintButton_Click(sender As Object, e As EventArgs) Handles _
PrintButton.Click
Dim PrintPath As String = System.Environment. _
GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
fileToPrint = New System.IO.StreamReader(PrintPath & "\myFile.txt")
printFont = New System.Drawing.Font("Arial", 10)
PrintDocument1.Print()
fileToPrint.Close()
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As _
System.Drawing.Printing.PrintPageEventArgs) Handles _
PrintDocument1.PrintPage
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
Dim line As String = Nothing
linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics)
While count < linesPerPage
line = fileToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * printFont.GetHeight(e.Graphics)
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _
yPos, New StringFormat())
count += 1
End While
If Not (line Is Nothing) Then
e.HasMorePages = True
End If
End Sub