As the name suggests, the PrintPage
event of the PrintDocument
prints a page. If you want to print multiple pages, you need to raise that event multiple times. You do that by setting the e.HasMorePages
property to True
in the event handler when there are more pages to print. Here's a simple example of printing one record per page from a DataTable
:
'The data to be printed.
Private table As DataTable
'The index of the row to print.
Private rowIndex As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Start printing at the first row.
rowIndex = 0
'Print the data.
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Get the current row.
Dim row = table.Rows(rowIndex)
'Print the current row.
e.Graphics.DrawString(row.Field(Of String)("SomeColumn"), Me.Font, Brushes.Black, 50, 50)
rowIndex += 1
'Print another page if and only if there is another row to be printed.
e.HasMorePages = rowIndex < table.Rows.Count
End Sub
You can modify the actual printing code in the PrintPage
event handler to print whatever data you want in whatever layout you want. If you have multiple values to print then you make multiple calls to e.Graphics.DrawX
methods. If it's all text you want to print then it will be all DrawString
calls, but you have DrawImage
for printing images and DrawRectangle
, etc, for printing shapes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…