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.
PrintForm pf = new PrintForm();
pf.Document = yourRichTextBox.rtf;
pf.windowtitle = "My Application";
pf.ShowDialog(this);
if (pf.error)
{
MyExceptionHandler(pf.printexception); / handle the exception
};
pf.Dispose(); / required if you use ShowDialog() rather than Show();
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