C# Print Dialog Box

C# Print Dialog Box

A user can use the Print dialog box to select a printer, configure it, and perform a print job. Print dialog boxes provide an easy way to implement Print and Print Setup dialog boxes in a manner consistent with Windows standards.

C# print-dialog-box

The Print dialog box includes a Print Range group of radio buttons that indicate whether the user wants to print all pages , a range of pages, or only the selected text. The dialog box includes an edit control in which the user can type the number of copies to print . By default, the Print dialog box initially displays information about the current default printer.

using System; using System.Drawing; using System.Windows.Forms; namespace Win dowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { PrintDialog dlg = new PrintDialog(); dlg.ShowDialog(); } } }

How to print a Document in C#?

How to print a Document in C#

PrintDocument object represents a document to be printed. It 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. Once a PrintDocument is created, we can set the Document property of PrintDialog as this document. After that we can also set other properties.

privatevoid PrintButton_Click(object sender, EventArgs e) { PrintDialog pDlg = newPrintDialog(); PrintDocument pDoc = newPrintDocument(); pDoc.DocumentName = "Print Document"; pDlg.Document = pDoc; pDlg.AllowSelection = true; pDlg.AllowSomePages = true; if (pDlg.ShowDialog() == DialogResult.OK) { pDoc.Print(); } else { MessageBox.Show("Print Cancelled"); } }

How to Format PrintDocument?

If you want to set the size of the paper:

printDialog.Document = printDocument; printDocument.DefaultPageSettings.PaperSize = new PaperSize("my Document", width, height); printDocument.DefaultPageSettings.Landscape = true;

How to Preview PrintDocument

With the help of PrintPreviewDialog You can preview a document also.

printPreviewDialog1.Document = printDocument; //Associate PrintPreviewDialog with PrintDocument. printPreviewDialog1.ShowDialog(); // Show PrintPreview Dialog