Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Text File send blank Page out

Status
Not open for further replies.

adalli

Programmer
Feb 8, 2005
44
MT
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
 
the handler is for .PrintPage, but you are calling .Print
 
It works with a small tweak - you need withevents for the objPrintDocument
Code:
    Private WithEvents objPrintDocument As New PrintDocument
    Private objStreamToPrint As StreamReader
    Private objPrintFont As Font


    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

        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
 
Thanks for your help, but still getting a blank page.

Is it possible I am missing a namespace or something?

Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing
 
nope, if you miss an imports it wouldn't compile.

if you do a withevents you don't really need to add a handler because it already exists. if you declare it withevent vs will show it in the list of controls and then you can choose the appropriate event from the list. and it will create the sub for you then just copy your code in that.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top