PRINT AND PREVIEW PAGE IN C#

Dematagoda kamal

Total Post:2

Points:14
Posted by  Dematagoda kamal
C# 
C#
 1724  View(s)
Ratings:
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)
            {
                printDocument1.Print();
            }
}
 
private void btnPrintPreview_Click(object sender, EventArgs e)
{
            //Associate PrintPreviewDialog with PrintDocument.
            printPreviewDialog1.Document = printDocument1; 
 
            // Show PrintPreview Dialog
            printPreviewDialog1.ShowDialog();
}

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..

Thanks

  1. AVADHESH PATEL

    Post:604

    Points:4228
    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;
            }
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!