Introduction
This example shows how to print and display print previews in VB.NET. It shows how to use the PrintDocument
object to print, how to print with the PrintDialog
control, and how to display a print preview with the PrintPreviewDialog
.
To print a document or provide a preview, the program creates a PrintDocument
object. It assigns event handlers to this object's BeginPrint
, QueryPageSettings
, PrintPage
, and EndPrint
events. PrintPage
is the one that generates the output and it's the only one that is required. In this program, subroutine PreparePrintDocument
creates the PrintDocument
and sets its PrintPage
event handler.
The event handler draws on the Graphics
object it is passed as a parameter. It uses the e.MarginBounds
parameter to learn where the margins are on the page. It finishes by setting e.HasMorePages
to False
to indicate that the printout is done.
Private Function PreparePrintDocument() As PrintDocument
Dim print_document As New PrintDocument
AddHandler print_document.PrintPage, AddressOf _
Print_PrintPage
Return print_document
End Function
Private Sub Print_PrintPage(ByVal sender As Object, ByVal e _
As System.Drawing.Printing.PrintPageEventArgs)
e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds)
Dim dotted_pen As New Pen(Color.Black, 5)
dotted_pen.DashStyle = Drawing2D.DashStyle.Dash
e.Graphics.DrawEllipse(dotted_pen, e.MarginBounds)
dotted_pen.Dispose()
Dim x0 As Integer = e.MarginBounds.X
Dim y0 As Integer = e.MarginBounds.Y
Dim wid As Integer = e.MarginBounds.Width
Dim hgt As Integer = e.MarginBounds.Height
Dim pts() As Point = { _
New Point(x0, y0 + hgt \ 2), _
New Point(x0 + wid \ 2, y0), _
New Point(x0 + wid, y0 + hgt \ 2), _
New Point(x0 + wid \ 2, y0 + hgt) _
}
e.Graphics.DrawPolygon(New Pen(Color.Black, 5), pts)
e.HasMorePages = False
End Sub
To display a print preview, the program uses the PreparePrintDocument
function to make a PrintDocument
object and saves the result in a PrintPreviewDialog
's Document
property. It calls the dialog's ShowDialog
method and the rest is automatic. The user can use the dialog to zoom in and out, examine the printouts different pages (in this program, the printout only has one page), and print the document.
Private Sub btnPrintPreview_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnPrintPreview.Click
dlgPrintPreview.Document = PreparePrintDocument()
dlgPrintPreview.WindowState = FormWindowState.Maximized
dlgPrintPreview.ShowDialog()
End Sub
To display the print dialog, the program uses the PreparePrintDocument
function to make a PrintDocument
object and saves the result in a PrintDialog
's Document
property. It calls the dialog's ShowDialog
method and the rest is automatic. The user can use the dialog to select the printer and change the printer's settings, and then launch or cancel the printout.
Private Sub btnPrintWithDialog_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnPrintWithDialog.Click
dlgPrint.Document = PreparePrintDocument()
dlgPrint.ShowDialog()
End Sub
To print the document immediately to the currently selected printer, the program uses the PreparePrintDocument
function to make a PrintDocument
object and calls that object's Print
method.
Private Sub btnPrintNow_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnPrintNow.Click
Dim print_document As PrintDocument = _
PreparePrintDocument()
print_document.Print()
End Sub
History
- 4th August, 2011: Initial version