Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

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:

  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)
    {
        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)

Share

About the Author

Chetan Sheladiya
Product Manager Metizsoft Solutions
India India
I am working as operation director ( New business development) with Metizsoft solution but I have worked as developer/Leader/manager for asp.net, C# and C++, win32 API , Image processing and Graphics for almost 7 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.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionThank you PinmemberMember 1044509713-Mar-14 9:40 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 25 Oct 2013
Article Copyright 2013 by Chetan Sheladiya
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid