Click here to Skip to main content
13,049,343 members (68,116 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 25 Oct 2013

RichTextBox Printing in C# with WinForms VS2010

, 25 Oct 2013
Rate this:
Please Sign up or sign in to vote.
Print Richtextbox data from Winform Application dotnet 2010


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


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:

  7. Setting up the Printdialog control properties as shown in images
  8. 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.
  9. 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. 

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.

private void button1_Click(object sender, EventArgs e)
    if (printDialog1.ShowDialog() == DialogResult.OK)

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);
        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)
            richTextBox1.Font, brush, x, y);
        y += 15;
        if (y >= e.MarginBounds.Bottom)
            e.HasMorePages = true;

    linesPrinted = 0;
    e.HasMorePages = false;

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


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


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


About the Author

Chetan Sheladiya
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, 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.

You may also be interested in...

Comments and Discussions

QuestionNot Get Proper Output Pin
Member 128403288-Nov-16 19:32
memberMember 128403288-Nov-16 19:32 
QuestionHow to set paper size to print in c# Pin
Member 1126786127-Nov-14 8:46
memberMember 1126786127-Nov-14 8:46 
GeneralNot fully baked solution? Pin
warnerja24-Oct-14 10:44
memberwarnerja24-Oct-14 10:44 
GeneralHI Pin
Misael Moneró Thompson18-Oct-14 13:15
memberMisael Moneró Thompson18-Oct-14 13:15 
QuestionThank you Pin
Member 1044509713-Mar-14 9:40
memberMember 1044509713-Mar-14 9:40 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 25 Oct 2013
Article Copyright 2013 by Chetan Sheladiya
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid