Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitlePrint and display print previews in VB .NET
DescriptionThis 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.
Keywordsprint, preview, print preview, VB .NET
CategoriesGraphics, VB.NET
 
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.

 
' Make and return a PrintDocument object that's ready
' to print the paragraphs.
Private Function PreparePrintDocument() As PrintDocument
    ' Make the PrintDocument object.
    Dim print_document As New PrintDocument

    ' Install the PrintPage event handler.
    AddHandler print_document.PrintPage, AddressOf _
        Print_PrintPage

    ' Return the object.
    Return print_document
End Function

' Print the next page.
Private Sub Print_PrintPage(ByVal sender As Object, ByVal e _
    As System.Drawing.Printing.PrintPageEventArgs)
    ' Draw a rectangle at the margins.
    e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds)

    ' Draw a thick, dashed ellipse.
    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()

    ' Draw a thick diamond.
    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)

    ' There are no more pages.
    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.
 
' Display a print preview.
Private Sub btnPrintPreview_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnPrintPreview.Click
    ' Make a PrintDocument and attach it to 
    ' the PrintPreview dialog.
    dlgPrintPreview.Document = PreparePrintDocument()

    ' Preview.
    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.
 
' Print with the print dialog.
Private Sub btnPrintWithDialog_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnPrintWithDialog.Click
    ' Make a PrintDocument and attach it to 
    ' the Print dialog.
    dlgPrint.Document = PreparePrintDocument()

    ' Display the print dialog.
    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.
 
' Print immediately.
Private Sub btnPrintNow_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnPrintNow.Click
    ' Make a PrintDocument object.
    Dim print_document As PrintDocument = _
        PreparePrintDocument()

    ' Print immediately.
    print_document.Print()
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated