Home > DeveloperSection > Articles > PrintDocument PrintPreviewDialog and PrintDialog Control in C#.Net

PrintDocument PrintPreviewDialog and PrintDialog Control in C#.Net


C# C# 
Ratings:
6 Comment(s)
 90062  View(s)
Rate this:

PrintDocument, PrintPreviewDialog and PrintDialog Control in C#.Net

A PrintDialog control is used to open the Windows Print Dialog. The PrintDocument component allows users to send an output to a printer. With the help of PrintPreviewDialog You can preview a document.

How to PrintDocument and check PrintPreview

Drag and drop PrintDocument control, PrintDialog control and PrintPreviewDialog control from toolbox on the WindowForm.

PrintDocument, PrintPreviewDialog and PrintDialog Control in C#.Net

PrintDocument, PrintPreviewDialog and PrintDialog Control in C#.Net

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

 

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();

}

Run the project

 

PrintDocument, PrintPreviewDialog and PrintDialog Control in C#.Net

 

When you click PrintPreview Button then PrintPreview dialog will open.

PrintDocument, PrintPreviewDialog and PrintDialog Control in C#.Net

When click Print Button then Print dialog will open.

PrintDocument, PrintPreviewDialog and PrintDialog Control in C#.Net

When you click Print Button then output will goes to the Printer.

 


printing text from a richtextbox

By priya  on   5 years ago
hai Pushpendra Singh
 u r article is is working but its printing only 1st page. then how to print remaining text in richtextbox.... its not moving to next page to print remaining text......... plz can u help me :-)

thanks

printing text from a richtextbox

By priya  on   5 years ago
problem is when we have more text than its fits in  one page....... then we have to print it in next page.... i think i have to use  e.HasMorePages = True to move to next page.......... but its not working. its only prints first page untilll text fits in first page..



thanks
priya
priyarebbe@gmail.com

printing text from a richtextbox

By Awadhendra Tiwari on   5 years ago
Hi priya
You have used e.HasMorePages=true. Its working but problem is that you have to put it in loop because at the end of first page it becomes false. Here I will give an algorithm which might be solve your problem.

private void print_doc(......)
{
      TextDocument doc = (TextDocument)sender;
      //Some code is written here.
      .
      .
      float x = e.MarginBounds.Left;   //set left margin.
      float y = e.MarginBounds.Top;   //set right margin.

        doc.PageNumber += 1;   //increasing page number by one.
      
        if (doc.Offset < doc.Text.GetUpperBound(0))  
        {
            e.HasMorePages = true;
        } else {
            doc.Offset = 0;
        }
}


You can modify this code according to your requirements and still if you get any problem then you can ask it with your sample code on mindstick forum or on here.
Thanks.

printing text from a richtextbox

By priya  on   5 years ago
hai awadhendra

 can i have ur email id.............. so i can forward my application............ i have few doubts
thanks
priyarebbe@gmail.com

printing text from a richtextbox

By Uttam Misra on   5 years ago
Priya,
Join MindStick Forum for technical discussion.

PrintDocument PrintPreviewDialog and PrintDialog Control in C#.Net

By Memtech Lodhi on   5 years ago

Thanks Puspendra,

Your article and discussion helped me alot.


Don't want to miss updates? Please click the below button!

Follow MindStick