print and preview page in C#

Total Post:2


 2243  View(s)
Rate this:

Hi Expert!

I want to print content of richtextbox control in c#
My line of code as below

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;         
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)
private void btnPrintPreview_Click(object sender, EventArgs e)
            //Associate PrintPreviewDialog with PrintDocument.
            printPreviewDialog1.Document = printDocument1; 
            // Show PrintPreview Dialog

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 until text fits in first page..


  1. Re: print and preview page in C#

    Hi Dematagoda!

    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;

      Modified On Mar-29-2018 06:27:54 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.