Sample prints the content of richTextBox
Imports
Code:
Imports System.Drawing.Printing
Imports System.IO
Declarations
Code:
Dim printDialog1 As PrintDialog
Dim WithEvents pDoc As PrintDocument
Dim myReader As StringReader
Dim line As String = Nothing
Print Button
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
printDialog1 = New PrintDialog
pDoc = New PrintDocument()
printDialog1.Document = pDoc
Dim strText As String = Me.richTextBox1.Text
myReader = New StringReader(strText)
If printDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
pDoc.Print()
End If
End Sub
Code:
Private Sub mdoc_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pDoc.BeginPrint
line = myReader.ReadLine
End Sub
Code:
Private Sub mdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pDoc.PrintPage
Dim yPosition As Single = 0 'initial position
Dim count As Integer = 0 'line counter
Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
Dim printFont As Font = Me.RichTextBox1.Font
'get number of lines per page
Dim linesPerPage As Single = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
' Iterate over the string using the StringReader, printing each line.
While count < linesPerPage And Not (line Is Nothing)
' get next line position
yPosition = (topMargin + (count * printFont.GetHeight(e.Graphics)))
'draw line
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPosition, New StringFormat())
count += 1
'get next line
line = myReader.ReadLine
End While
' If there are more lines, print another page.
If Not (line Is Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub