How to print selected text of richtextbox?

rfresh

Active member
Joined
Aug 23, 2012
Messages
26
Programming Experience
1-3
Here is my code to print a richtextbox file.

But I have two problems:

1. It doesn't print any text. Just blank paper comes out of my printer.

2. I'd like to be able to optionally select text to print as an option. I've enabled that in my printDialog component.

Thanks for any help.

C#:
            PrintDocument printDoc = new PrintDocument();
            printDoc.DocumentName = mFullPathFileName;
            printDialog.Document = printDoc;
            DialogResult result = printDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                printDoc.Print();
            }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
Have you handled the PrintPage event of your PrintDocument? That's where you actually tell it what to print. There are plenty of examples around of that. Try them first and post back if you have further issues.

You can specify whatever you like to be printed. If you want to print the text that's selected in a RichTextBox then specify the SelectedText of that RichTextBox when you call DrawString. Just keep in mind that the SelectedText is just text and will not include any formatting. If you want formatting then you'll need to use the SelectedRtf to determine what formatting has been applied to that text and then apply it to your printing. That's not a trivial exercise though. If that's what you want then it might pay to visit MSDN and check the example of a RichTextBox with built-in WYSIWYG printing it provides.
 
Top Bottom