Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.IO
Public Class PrintDoc
Private printFont As Font
Private streamToPrint As StreamReader
Public Sub PrintFile(ByVal strFileToPrint As String)
Try
streamToPrint = New StreamReader(strFileToPrint)
Try
printFont = New Font("Lucida Console", 9)
Dim pd As PrintDocument = New PrintDocument()
AddHandler pd.PrintPage, New System.Drawing.Printing.PrintPageEventHandler(AddressOf Me.pd_PrintPage)
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show("An error occurred printing the file - " + ex.Message)
End Try
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
Dim lpp As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = 30
Dim topMargin As Single = 60
Dim line As String
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
line = streamToPrint.ReadLine()
While (count < lpp And line <> Nothing)
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics))
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count = count + 1
If (count < lpp) Then
line = streamToPrint.ReadLine()
End If
End While
If (line <> Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
End Class
Things go better with rock...