A PrintDialog control is used to open the Windows Print Dialog. The PrintDocument component allows users to send an output to a printer. With 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 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 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 Print preview button then preview will show.

PrintDocument, PrintPreviewDialog and PrintDialog Control in VB.Net

When click print button then print dialog will open.

PrintDocument, PrintPreviewDialog and PrintDialog Control in VB.Net

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

 

  Modified On Mar-22-2018 07:17:49 AM
  1. I want to printDocument in VB.NET from csv file.With OpenFileDialog choose the correct csv file and after that I want to print it on my default printer.
    Thanks.
    Dim.

Leave Comment