Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

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.

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#?

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

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More