Home > DeveloperSection > Forums > print and preview page in C#
Dematagoda kamal
Dematagoda kamal

Total Post:2

Points:14
Posted on    February-12-2013 1:07 AM

 C# C# 
Ratings:


 1 Reply(s)
 1343  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)
            {
                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



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    February-12-2013 8:43 AM

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;
        }
}

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

Follow MindStick