Printing selected text in Rich Text Box using C#

Posted on 2009-02-24
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


    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

    Thanks for your time

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now