Click here to Skip to main content
11,583,532 members (67,509 online)
Click here to Skip to main content

Tagged as

How to Print Data from Datagrid with All Pages (from Paging) in Silverlight with VB.NET

, 5 Aug 2011 CPOL 14.1K 1.1K 12
Rate this:
Please Sign up or sign in to vote.
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.

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.

' 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

History

  • 4th August, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sathishsingam
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 2 Pin
JP®19-Mar-12 3:36
memberJP®19-Mar-12 3:36 
GeneralMy vote of 1 Pin
Росен Христов18-Jan-12 3:15
memberРосен Христов18-Jan-12 3:15 
QuestionSilverlight? Pin
Jay Douglas10-Aug-11 7:10
memberJay Douglas10-Aug-11 7:10 
GeneralMessage Removed Pin
sathishsingam10-Aug-11 23:43
membersathishsingam10-Aug-11 23:43 
GeneralRe: Silverlight? Pin
Росен Христов18-Jan-12 3:10
memberРосен Христов18-Jan-12 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 5 Aug 2011
Article Copyright 2011 by sathishsingam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid