PrintDocument with streamreader gives blank pages

R

ReidarT

I use this code to print a textfile, but it gives me a lot of blank pages.
The file contains 9 lines of text.

Private strFileName As String = "C:\Sendepost\LoggFil.txt"
Private objStreamToPrint As StreamReader
Private objPrintFont As Font

Private Sub cmdSkrivUt_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSkrivUt.Click
Dim objPrintDocument As PrintDocument = New PrintDocument
objPrintDocument.DocumentName = "Utskrift av logg"
PrintDialog1.AllowPrintToFile = False
PrintDialog1.AllowSelection = True
PrintDialog1.AllowSomePages = False
PrintDialog1.Document = objPrintDocument

If PrintDialog1.ShowDialog() = DialogResult.OK Then
objStreamToPrint = New StreamReader(strFileName)
objPrintFont = New Font("Arial", 10, FontStyle.Regular)
AddHandler objPrintDocument.PrintPage, AddressOf
objPrintDocument_PrintPage
objPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings
objPrintDocument.Print()
objStreamToPrint.Close()
objStreamToPrint = Nothing
End If

End Sub

Private Sub objPrintDocument_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim sngLinesPerpage As Single = 0
Dim sngVerticalPosition As Single = 0
Dim intLineCount As Integer = 0
Dim sngLeftMargin As Single = e.MarginBounds.Left
Dim sngTopMargin As Single = e.MarginBounds.Top
Dim strLine As String

sngLinesPerpage = _
e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)
strLine = objStreamToPrint.ReadLine()
While (intLineCount < sngVerticalPosition And strLine <> Nothing)
sngVerticalPosition = sngTopMargin + _
(intLineCount * objPrintFont.GetHeight(e.Graphics))

e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, _
sngLeftMargin, sngVerticalPosition, New StringFormat)


intLineCount = intLineCount + 1

If (intLineCount < sngLinesPerpage) Then
strLine = objStreamToPrint.ReadLine()
End If
End While

If (strLine <> Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If

End Sub

regards
reidarT
 
W

William LaMartin

One thing I see that is probably wrong is this line

While (intLineCount < sngVerticalPosition And strLine <> Nothing)

I think it should read While (intLineCount < sngLinesPerpage And strLine <>
Nothing).

Try that change and see if it helps.
 
R

ReidarT

Thanks alot. It works perfectly.
reidarT
William LaMartin said:
One thing I see that is probably wrong is this line

While (intLineCount < sngVerticalPosition And strLine <> Nothing)

I think it should read While (intLineCount < sngLinesPerpage And strLine
<> Nothing).

Try that change and see if it helps.
 

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