Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

RichTextBox Printing in C# with WinForms VS2010

4.83/5 (8 votes)
25 Oct 2013CPOL1 min read 63.6K   2.6K  
Print Richtextbox data from Winform Application dotnet 2010

Introduction

I have tried to search and download few application but that code is working but there is rare guide available for the setting up the printcontrol and adding code for the method I will try to elaborate as much so any one can easily update code even after latest version of Visual Studio

Background

Just article you can try to use for the multiline text box or richtextbox control or with just minor tweaks you can use for any custom control too.

Using the code

First of all I would say down load the source code and I will traverse you across that .

  1. Create a Visual Studio WinForms project
  2. Place the richtextbox over Form from the toolbox panel 
  3. Add (Drag on form) the PrintDialog from the  toolbox => printing panel  on the Form 
  4. Add the PrintDocument from the   toolbox => printing panel  on the Form 
  5. Add the Button.
  6. Now your Windows Would look like this:

    Image 1

  7. Setting up the Printdialog control properties as shown in images
  8. Image 2

  9. Setting up the PrintDocument Control  Properties as shown in images  but to setup this you need to add the OnPrintPage event line in the Form1.Designer.cs.
  10. C#
    this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.OnPrintPage);

So while setting up property the same event will show there or you can double click on the listed event it will create the back end method in the code. 

Image 3

Now have a look at the code its very simple to get idea. simple print code behind the print button and that will call all printDocument1_BeginPrint and OnPrintPage page methods.

C#
private void button1_Click(object sender, EventArgs e)
{
    if (printDialog1.ShowDialog() == DialogResult.OK)
    {
        printDocument1.Print();
    }
}

private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    char[] param = { '\n' };

    if (printDialog1.PrinterSettings.PrintRange == PrintRange.Selection)
    {
        lines = richTextBox1.SelectedText.Split(param);
    }
    else
    {
        lines = richTextBox1.Text.Split(param);
    }

    int i = 0;
    char[] trimParam = { '\r' };
    foreach (string s in lines)
    {
        lines[i++] = s.TrimEnd(trimParam);
    }
}

private int linesPrinted;
private string[] lines;

private void OnPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    int x = e.MarginBounds.Left;
    int y = e.MarginBounds.Top;
    Brush brush = new SolidBrush(richTextBox1.ForeColor);

    while (linesPrinted < lines.Length)
    {
        e.Graphics.DrawString(lines[linesPrinted++],
            richTextBox1.Font, brush, x, y);
        y += 15;
        if (y >= e.MarginBounds.Bottom)
        {
            e.HasMorePages = true;
            return;
        }
    }

    linesPrinted = 0;
    e.HasMorePages = false;
}

Download and run the code and let me know if you have any question or queries.

History

Hopefully no revision needed but if needed I am happy to update it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)