ARTICLE

PrintDocument,PrintDialog and PrintPreviewDialog control in VB.NET

Posted by Dea Saddler Articles | ASP.NET using VB.NET March 25, 2011
In this article you will learn about PrintDocument,PrintDialog and PrintPreviewDialog control and how you can print a document and check PrintPreview using these controls.
Reader Level:


PrintDocument:-

A visual basic PrintDocument control is used to represent a printed document. The PrintDocument object encapsulate all the information needed to print a Document. It handles the Event and operations of printing. PrintDocument exposes three properties which are given below


Properties of PrintDocument control:-

  • PrintController:- Describe how each page is guided through the printing process. 
  • PrinterSettings:-contains information about the capabilities and setting of the printer. 
  • DefaultPageSettings:-Encapsulates the configuration for printing each printed page.
     

PrintDialog:-

A visual basic PrintDialog control is used to print the Document. PrintDialog control is supported by the PrintDialog class. If you want to print a document you need to set the document property of the PrintDialog to PrintDocument object and PrinterSettings to PrinterSettings Object.


Properties of PrintDialog Control:-
  • Document:- This property determines the PrintDocument used to obtain PrinterSettings. 
  • PrintToFile:- This property determines Whether the Print to file CheckBox is enabled. 
  • PrinterSettings:- This property sets the PrinterSettings dialog box to modify.
     

PrintPreviewDialog:-

A visual basic PrintPreviewDialog control is used to preview the document before printing. PrintPreviewDialog is supported by PrintPreviewDialog class. You can preview a document by setting the document property of the PrintPreviwDialog object to the PrintDocument object. Once you set, the PrintPreviewDialog provide the functionality to Zoom, Print, to view multiple pages of the preview etc.


Properties of PrintPreviewDialog control:-
  • Rows:- Determines the number of pages displayed Vertically.
     
  • Columns:-Determines the number of pages displayed Horizontally.
     
  • Zoom:- Determines a value specifing how large the pages will appear.



    Here we are taking an example that how you can Print a document and check PrintPreview.
     
  • Open a new project.
  • Drag the two Buttons, PrintDocument, PrintDialog, PrintPreviewDialog control on form, the form will look like given below.

    Print1.gif

    Print1.1.gif

     
  • Write the code given below.

       Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
            e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, 100, 100)
            e.Graphics.PageUnit = GraphicsUnit.Inch
        End Sub
            'open the print dialog on Print Button click
        Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
            PrintDialog1.Document = PrintDocument1 'PrintDialog associate with PrintDocument.
            If PrintDialog1.ShowDialog() = DialogResult.OK Then
                PrintDocument1.Print()
            End If
        End Sub
            'open the print preview on PrintPreview Button click
        Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
            PrintPreviewDialog1.Document = PrintDocument1 'PrintPreviewDialog associate with PrintDocument.
            PrintPreviewDialog1.ShowDialog() 'open the print preview
        End
    Sub

     Output:-

    print2.gif
    Print2.1.gif

    Print3.gif
    Print3.1.gif


     

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor