Print Preview and Print functionality Code in Windows C# application

Raj.Trivedi
Posted by Raj.Trivedi under C# category on | Points: 40 | Views : 40463
Hello Friends,

Drag and Drop PrintDialog ,Print Preview Dialog box and print document on the window form.

Add the namespace to your code behind.

using System.Drawing.Printing;

This name space will allow us to use print functionality.

// This will call the print dialog box to select the printer.

private void printToolStripMenuItem_Click(object sender, EventArgs e)
{

printDialog1.Document = printDocument;

if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument.Print();
}
}



// This will allow us to see print preview before printing.

private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument;
printPreviewDialog1.ShowDialog();


}


// This will send the Rich Text Box data to print.
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);
e.Graphics.PageUnit = GraphicsUnit.Inch;
}

Comments or Responses

Login to post response