articles

Home / DeveloperSection / Articles / PrintDocument PrintPreviewDialog and PrintDialog Control in VB.Net

PrintDocument PrintPreviewDialog and PrintDialog Control in VB.Net

PrintDocument PrintPreviewDialog and PrintDialog Control in VB.Net

Pushpendra Singh 150671 13-Dec-2010

The PrintDialog control is used to open the Windows Print Dialog. A PrintDocument component allows users to send an output to a printer. By the help of PrintPreviewDialog, You can preview a document.

How to PrintDocument and check PrintPreview in VB.Net.

Drag and drop PrintDocument control, PrintDialog control and PrintPreviewDialog control from the toolbox on the WindowForm.

PrintDocument PrintPreviewDialog and PrintDialog Control in VB.Net

PrintDocument PrintPreviewDialog and PrintDialog Control in VB.Net

PrintDocument:  The PrintDocument object encapsulates all the information needed to print a page. They associate with the control which content can be print.  They handle the events and operations of printing.

Step1: Associate control to print the document

  
  Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, 100, 100)
 
        e.Graphics.PageUnit = GraphicsUnit.Inch
 End Sub

 Step2 Write code for printing and print preview. 

'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
 
'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

 Run the project

PrintDocument PrintPreviewDialog and PrintDialog Control in VB.Net

When you click the Print Preview button then the preview will show.

PrintDocument PrintPreviewDialog and PrintDialog Control in VB.Net

When clicking the print button then the print dialog will open.

PrintDocument PrintPreviewDialog and PrintDialog Control in VB.Net

When you click print then the output will go to the printer.

[You should Read also this Article - ToolBarTray Control in WPF 


Updated 24-May-2020

Leave Comment

Comments

Liked By