Printing selected text in Rich Text Box using C#

Posted on 2009-02-24
Medium Priority
Last Modified: 2013-12-17
Hello experts,

I am using Extended RichTextBox control from Microsoft.
Even if I check selection or specify page range  in Print Dialog the complete text is printed. I am unable to figure out a way to handle this. Can some one help me please with a sample code.

Thanks a lot in advance
LVL 22

Accepted Solution

pivar earned 2000 total points
ID: 23727660

Substitute the methods printDoc_BeginPrint and printDoc_PrintPage in the example, with attached methods. This is a sample showing how you can print a selection. You'll may check PrintDocument.PrinterSettings.PrintRange what is going to be printed.


    private void printDoc_BeginPrint(object sender,
        System.Drawing.Printing.PrintEventArgs e) {
      // Start at the beginning of the text
      m_nFirstCharOnPage = myRichTextBoxEx.SelectionStart;
    private void printDoc_PrintPage(object sender,
        System.Drawing.Printing.PrintPageEventArgs e) {
      // To print the boundaries of the current page margins
      // uncomment the next line:
       e.Graphics.DrawRectangle(System.Drawing.Pens.Blue, e.MarginBounds);
      // make the RichTextBoxEx calculate and render as much text as will
      // fit on the page and remember the last character printed for the
      // beginning of the next page
       m_nFirstCharOnPage = myRichTextBoxEx.FormatRange(false,
                                               myRichTextBoxEx.SelectionStart+ myRichTextBoxEx.SelectionLength);
      // check if there are more pages to print
      if (m_nFirstCharOnPage < myRichTextBoxEx.SelectionStart + myRichTextBoxEx.SelectionLength)
        e.HasMorePages = true;
        e.HasMorePages = false;

Open in new window


Author Closing Comment

ID: 31550511
Thanks for your time

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question