Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#
Tip/Trick

RichTextBox Printing in C# with WinForms VS2010

Rate me:
Please Sign up or sign in to vote.
4.83/5 (8 votes)
25 Oct 2013CPOL1 min read 61.5K   2.6K   8   6
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)


Written By
Program Manager CentralizeMedia
United States United States
I am working as operation director ( New business development) with Centralizemedia but I have worked as developer/Leader/manager for asp.net, C# and C++, win32 API , Image processing and Graphics for almost 10 years.

As part of education I have bachelor in Computer Engg and Master in Business Management,In free time I would love to watch movie and Business magazine.

Comments and Discussions

 
GeneralMy vote of 5 Pin
theStarter19-Sep-17 17:52
theStarter19-Sep-17 17:52 
QuestionNot Get Proper Output Pin
Member 128403288-Nov-16 19:32
Member 128403288-Nov-16 19:32 
QuestionHow to set paper size to print in c# Pin
Member 1126786127-Nov-14 8:46
Member 1126786127-Nov-14 8:46 
GeneralNot fully baked solution? Pin
warnerja24-Oct-14 10:44
warnerja24-Oct-14 10:44 
GeneralHI Pin
Misael Moneró Thompson18-Oct-14 13:15
Misael Moneró Thompson18-Oct-14 13:15 
QuestionThank you Pin
Member 1044509713-Mar-14 9:40
professionalMember 1044509713-Mar-14 9:40 
Thank you sir it had helped me a lot... and i Hope you keep posting such artical and help the biggners...thank you very much...

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.