Hallo
so:
Public Class PrintingExample1
Inherits System.Windows.Forms.Form
…
private printFont As Font
private streamToPrint As StreamReader
Public Sub New ()
MyBase.New
InitializeComponent()
End Sub
'Event fired when the user presses the print button
Private Sub printButton_Click(sender As object, e As System.EventArgs)
Try
streamToPrint = new StreamReader („PrintMe.Txt“)
Try
printFont = new Font(„Arial“, 10)
Dim pd as PrintDocument = new PrintDocument() 'Assumes the default printer
AddHandler pd.PrintPage, 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
'Event fired for each page to print
Private Sub pd_PrintPage(sender As object, 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 = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line as String
'Work out the number of lines per page
'Use the MarginBounds on the event to do this
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
'Now iterate over the file printing out each line
'NOTE WELL: This assumes that a single line is not wider than the page width
'Check count first so that we don’t read line that we won’t print
line=streamToPrint.ReadLine()
while (count Nothing)
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics))
'Print Preview control will not work.
ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin, _
yPos, new StringFormat())
count = count + 1
if (count Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
…
End Class