articles

Home / DeveloperSection / Articles / The PrintPreviewDialog and PrintDialog Control in C#.Net

The PrintPreviewDialog and PrintDialog Control in C#.Net

The PrintPreviewDialog and PrintDialog Control in C#.Net

Pushpendra Singh 128862 25-Jan-2011

PrintDocument PrintPreviewDialog and PrintDialog Control in C#.Net

Where, The PrintDialogControls are 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

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

The PrintPreviewDialog and PrintDialog Control in C#.Net

The PrintPreviewDialog and PrintDialog Control in C#.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 void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
           e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);
           e.Graphics.PageUnit = GraphicsUnit.Inch;          
}

 

Step2:  write code for printing and print preview.

 

private void btnPrint_Click(object sender, EventArgs e)
{
            //PrintDialog associate with PrintDocument;
            printDialog1.Document = printDocument1;
 
            if (printDialog1.ShowDialog()==DialogResult.OK)
            {
                printDocument1.Print();
            }
}
 
private void btnPrintPreview_Click(object sender, EventArgs e)
{
            //Associate PrintPreviewDialog with PrintDocument.
            printPreviewDialog1.Document = printDocument1;  
  
            // Show PrintPreview Dialog
            printPreviewDialog1.ShowDialog();
}
Run the project

 

The PrintPreviewDialog and PrintDialog Control in C#.Net

 

When you click PrintPreview Button then PrintPreview dialog will open.

The PrintPreviewDialog and PrintDialog Control in C#.Net

When click Print Button then Print dialog will open.

The PrintPreviewDialog and PrintDialog Control in C#.Net

When you click Print Button then output will goes to the Printer.

 


Updated 30-May-2020

Leave Comment

Comments

Liked By