Click here to Skip to main content
15,888,351 members
Articles / Programming Languages / C#
Tip/Trick

A Simple RTF Print Form

Rate me:
Please Sign up or sign in to vote.
4.55/5 (5 votes)
1 Oct 2016CPOL3 min read 27.1K   3.2K   17   4
A WYSIWYG Printer Interface for Rich text

Introduction

This form class adds complete basic multipage printing and page preview to any application needing to print rich text.

Background

The project started when I was looking for a way to print the contents of a rich text box control. I initially found the implementation provide by Martin Muller on MSDN (available here).

However, this works only for a single page of rich text and doesn't include any pagination support. Then I found a more complete soultion, the RichTextBoxHelper Class by M.Rojas available here, which has the needed graphics rendering support for proper pagination. I extended the class to include Page settings and Print Settings dialogs and Page preview, so it provides everything needed for standard printing interface. It also exposes any exceptions so they can be handled by the calling program, with a ref Exception parameter. Then I wrapped it in a small form that allows default printer selection, which can be added to any project.  As described below, the newest version is encapsulated in a DLL.

Using the Code

To use the form, simple add PrintForm.cs to your project with its attached PrintForm.designer.cs and Printform.resx. Also add the RichTextBoxPrintHelper.cs class. Change the namespace references in the PrintForm.cs and designer.cs code to match the project namespace. Then, all that is needed when you want to print the contents of a rich text box, is to create a new PrintForm, pass the rich text box contents to it as a string, and open the form with ShowDialog(this). The form buttons are self-explanatory. To change the default printer, click on the displayed printer name for a drop-box of installed printers.

C#
// Create The Form, pass the richtext, and window title, and call it

PrintForm pf = new PrintForm();
pf.Document = yourRichTextBox.rtf;
pf.windowtitle = "My Application";
pf.ShowDialog(this); // center form in parent app
if (pf.error)
 { 
    MyExceptionHandler(pf.printexception); / handle the exception
 };
pf.Dispose(); / required if you use ShowDialog() rather than Show();

// The richtext can be also passed in the constructor, if desired, via an override

Printform pf = pf(YourRichTextBox.rtf);
pf.ShowDialog(this);

In the latest version, the PrintForm is incorporated into printhelper.dll, which makes it simpler to add it to a project, and has new overloads for PageSettings (Margins, landscape or portrait mode) and adding a header with page numbering. To use it, copy the printhelper.dll to your project folder, then Use Add Item and Add Reference to add the file to your project. Include "using printhelper.dll;" in the header and create an instance of PrintTools, as in PrintTools pt = new PrintTools(). Then, you can use 1 of 3 overloads of GeneralPrintForm() to print your rtf as in pt.GeneralPrintForm("Title",RichTextBox1.Rtf,ref ex). The source code for the DLL and the demo is documented. You can still use the PrintForm itself, if you want, as outlined above. Just copy the files from the source code into your project and correct the namespace references.

Points of Interest

I found it surprising that there is no simple way to print the contents of a rich text box included in the C# managed code classes, given the extensive support for many other common tasks that makes the environment so easy to use for novice programmers. The PrintForm wrapper class largely solves the problem with the exception of more sophisticated features such as fixed page breaks, headers and footers found in a true word processor. Adding these to the standard rich text box control would complete the project.

The latest version now includes support for Page Settings and the ability to add a document header with page numbers. Many thanks to Andrew for this stackoverflow article:

History

  • 10-12-2014: Initial version 1.0.0.1
  • 09-28-2016: Version 1.0.0.3

License

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


Written By
United States United States
I am a retired MD who enjoys programming.

Comments and Discussions

 
PraiseThanks Pin
Jarmo Haka24-May-22 20:48
Jarmo Haka24-May-22 20:48 
GeneralRe: Thanks Pin
Howard 944849026-May-22 9:11
Howard 944849026-May-22 9:11 
GeneralThanks for sharing if you ever wanted to expand this Pin
Craig P Williams Sr14-Sep-21 5:49
professionalCraig P Williams Sr14-Sep-21 5:49 
GeneralRe: Thanks for sharing if you ever wanted to expand this Pin
Howard 944849014-Sep-21 10:48
Howard 944849014-Sep-21 10:48 

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.