Hi,
I am using the following code in order to be able to print all the contents of a text file. For some reason I am getting a blank page instead of the contents which exists in the file.
I have added a handler:
AddHandler objPrintDocument.PrintPage, AddressOf objPrintDocument_PrintPage
which while debuggng it, is not being called.
Can any one please help.
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim objPrintDocument As PrintDocument = New PrintDocument
Dim strFileName As String
'Print the file
strFileName = "C:\ErrorLog.txt"
objPrintDocument.DocumentName = "Printing Error Log File"
objStreamToPrint = New StreamReader(strFileName)
objPrintFont = New Font("Arial", 11)
AddHandler objPrintDocument.PrintPage, _
AddressOf objPrintDocument_PrintPage
objPrintDocument.Print()
objStreamToPrint.Close()
objStreamToPrint = Nothing
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 sngLeftMargin As Single = e.MarginBounds.Left
Dim sngTopMargin As Single = e.MarginBounds.Top
Dim intLineCount As Integer = 0
Dim strLine As String
'Work out the number of lines per page.
sngLinesperPage = e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)
If sngLinesperPage < 0 Then sngLinesperPage = 55
'Iterate through the file printing all lines
strLine = objStreamToPrint.ReadLine
While (intLineCount < sngLinesperPage And Not (strLine Is Nothing))
'Calculate the vertical position on the page
sngVerticalPosition = sngTopMargin + (intLineCount * objPrintFont.GetHeight(e.Graphics))
'Increment the line count
intLineCount = intLineCount + 1
If (intLineCount < sngLinesperPage) Then
strLine = objStreamToPrint.ReadLine
End If
End While
'If there are nore lines then print another page
If (strLine <> Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
I am using the following code in order to be able to print all the contents of a text file. For some reason I am getting a blank page instead of the contents which exists in the file.
I have added a handler:
AddHandler objPrintDocument.PrintPage, AddressOf objPrintDocument_PrintPage
which while debuggng it, is not being called.
Can any one please help.
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim objPrintDocument As PrintDocument = New PrintDocument
Dim strFileName As String
'Print the file
strFileName = "C:\ErrorLog.txt"
objPrintDocument.DocumentName = "Printing Error Log File"
objStreamToPrint = New StreamReader(strFileName)
objPrintFont = New Font("Arial", 11)
AddHandler objPrintDocument.PrintPage, _
AddressOf objPrintDocument_PrintPage
objPrintDocument.Print()
objStreamToPrint.Close()
objStreamToPrint = Nothing
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 sngLeftMargin As Single = e.MarginBounds.Left
Dim sngTopMargin As Single = e.MarginBounds.Top
Dim intLineCount As Integer = 0
Dim strLine As String
'Work out the number of lines per page.
sngLinesperPage = e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)
If sngLinesperPage < 0 Then sngLinesperPage = 55
'Iterate through the file printing all lines
strLine = objStreamToPrint.ReadLine
While (intLineCount < sngLinesperPage And Not (strLine Is Nothing))
'Calculate the vertical position on the page
sngVerticalPosition = sngTopMargin + (intLineCount * objPrintFont.GetHeight(e.Graphics))
'Increment the line count
intLineCount = intLineCount + 1
If (intLineCount < sngLinesperPage) Then
strLine = objStreamToPrint.ReadLine
End If
End While
'If there are nore lines then print another page
If (strLine <> Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub