Subscribe For Free Updates!

We'll not spam mate! We promise.

Oct 9, 2012

Print Document In Windows Forms Using C#

Views:

To Day I will show you How to Print a Document   in Windows Forms (.NET) using C# .

For Printing a Document or text From Text box or Data From Grid View we Use Print Dialog  Control From Toolbox tab in Windows Forms.
A Print Dialog control is used to open the Windows Print Dialog.
This Control  allows users to send an output to a printer. With the help of Print Preview Dialog You can preview a document Before Print it (Like in MS word).


So, Let Start
Drag and drop PrintDocument control, PrintDialog control and PrintPreviewDialog control from toolbox on the WindowForm.
Like Below Image ,

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
//Just Double Click printDocument1 Control 
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();

}

Now Press F5 To Run Project .
And You will See Following Outputs
When you click PrintPreview Button then PrintPreview dialog will open.



Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

1 comments:

  1. The Menu WinForms Control offers much more amazing navigation menus including print.
    http://www.kettic.com/winforms_ui/menu.shtml

    ReplyDelete

Become a Fan

visual studio learn