SIGN UP MEMBER LOGIN:    
ARTICLE

PrintDialog in C#

Posted by Mahesh Chand Articles | Windows Controls C# July 13, 2010
A PrintDialog control is used to open the Windows Print Dialog and let user select the printer, set printer and paper properties and print a file.
Reader Level:


A PrintDialog control is used to open the Windows Print Dialog and let user select the printer, set printer and paper properties and print a file. A typical Open File Dialog looks like Figure 1 where you select a printer from available printers, set printer properties, set print range, number of pages and copies and so on. Clicking on OK button sends the document to the printer.

PrintDlgImg1.jpg
Figure 1

Creating a PrintDialog

We can create a PrintDialog at design-time as well as at run-time.

Design-time

To create a PrintDialog control at design-time, you simply drag and drop a PrintDialog control from Toolbox to a Form in Visual Studio. After you drag and drop a PrintDialog on a Form, the PrintDialog looks like Figure 2.

PrintDlgImg2.jpg
Figure 2

Run-time

Creating a PrintDialog control at run-time is simple. First step is to create an instance of PrintDialog class and then call the ShowDialog method. The following code snippet creates a PrintDialog control.

PrintDialog PrintDialog1 = new PrintDialog();

PrintDialog1.ShowDialog();


Printing Documents


PrintDocument object represents a document to be printed. Once a PrintDocument is created, we can set the Document property of PrintDialog as this document. After that we can also set other properties.  The following code snippet creates a PrintDialog and sends some text to a printer.

private void PrintButton_Click(object sender, EventArgs e)

{

    PrintDialog printDlg = new PrintDialog();

    PrintDocument printDoc = new PrintDocument();

    printDoc.DocumentName = "Print Document";

    printDlg.Document = printDoc;

    printDlg.AllowSelection = true;

    printDlg.AllowSomePages = true;

    //Call ShowDialog

    if (printDlg.ShowDialog() == DialogResult.OK)

        printDoc.Print();

 

}

 

More Readings

I do not want to duplicate the work but here are various articles on Printing.

 

Summary

A PrintDialog control allows users to launch Windows Open File Dialog and let them select files. In this article, we discussed how to use a Windows Open File Dialog and set its properties in a Windows Forms application.

share this article :
post comment
 

private void PrintButton_Click(object sender, EventArgs e) { PrintDialog printDlg = new PrintDialog(); PrintDocument printDoc = new PrintDocument(); printDoc.DocumentName = "Print Document"; printDlg.Document = printDoc; printDlg.AllowSelection = true; printDlg.AllowSomePages = true; //Call ShowDialog if (printDlg.ShowDialog() == DialogResult.OK) printDoc.Print(); } above code I tryed to set max copy count in to print dialog & also AllowSelection = true ; is not working

Posted by manjula Aruggoda Mar 15, 2011

Nb, this PrintDialog is from the System.Windows.Forms namespace, not System.Windows.Controls. Thanks for the article!

Posted by Gabor Farkas Feb 28, 2011

Hi this is deepak i am trying to print the contant of richText Box ,i have used many lables in richbox all lables takes data from database when print button to print the result but i am getting only blank page no any data why here is my code plz tell me whats there problem on my ID "Deepak.pandit2@gmail.com"reply soon plzzzzz. [ protected void printToolStripMenuItem_Click(object sender, System.EventArgs e) { try { string strText = this.richTextBox1.Text; myReader = new StringReader(strText); PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog(); printPreviewDialog1.Document = this.ThePrintDocument; printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D; printPreviewDialog1.ShowDialog(); } catch (Exception exp) { System.Console.WriteLine(exp.Message.ToString()); } } protected void ThePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev) { float linesPerPage = 0; float yPosition = 0; int count = 0; float leftMargin = ev.MarginBounds.Left; float topMargin = ev.MarginBounds.Top; string line = null; Font printFont = this.richTextBox1.Font; SolidBrush myBrush = new SolidBrush(Color.Black); // Work out the number of lines per page, using the MarginBounds. linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); // Iterate over the string using the StringReader, printing each line. while (count < linesPerPage && ((line = myReader.ReadLine()) != null)) { // calculate the next line position based on the height of the font according to the printing device yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics)); // draw the next line in the rich edit control ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat()); count++; } // If there are more lines, print another page. if (line != null) ev.HasMorePages = true; else ev.HasMorePages = false; myBrush.Dispose(); } private void fileToolStripMenuItem_Click(object sender, EventArgs e) { } protected void exitToolStripMenuItem_Click(object sender, EventArgs e) { printDialog1.Document = ThePrintDocument; string strText = this.richTextBox1.Text; myReader = new StringReader(strText); if (printDialog1.ShowDialog() == DialogResult.OK) { try { this.ThePrintDocument.Print(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { MessageBox.Show("Printer nt inistall"); } } } ]

Posted by shashi gandhi Dec 15, 2010

You're welcome David.

Posted by Mahesh Chand Nov 07, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor