Click here to Skip to main content
11,805,604 members (55,483 online)
Click here to Skip to main content

Simplified .NET Printing in C#

, 12 Jan 2005 312.3K 18.3K 76
Rate this:
Please Sign up or sign in to vote.
Uses a RichtextBox to cache our text for printing.


This is a .NET approach to Simplified Printing in which, we are going to use a RichTextBox to cache all of our text for printing in printDocument.

Sample screenshot

RichTextBox is our "TextCache"

When I print a file, for example, using a RichTextBox, and printDocument, I drag a richTextBox onto our Windows Form, and then I place the control where it can be hidden. RichTextBox's visible property can be set to false, and its physical dimensions on the form can be made very small. We can write to this control in a number of ways. The RichTextBox.LoadFile will copy a text file to this control, or I can pass a string to the RichTextBox.Text property, and there after use the AppendText method at any point in the code. If I am required to loop through a file, I can write a record to the control, using AppendText at every iteration. I can write literals to the control whenever necessary.

If I print a report ,for example, I write the date and header information at the top of the document, and then use richTextBox.AppendText any time I add text to the report. This greatly simplifies the printing process, and gives the developer an intuitive feel for printing in the .NET environment.

The rest of this Print method is fairly straightforward, and is available from any textbook, or "Microsoft Whitepapers". I have used a Main Menu at the top of the Windows Form with sub menu items as "PageSetup", "PrintPreview", and "Print". The "Print_Click" fires the "OnBeginPrint", and the "OnPrintPage" events. Below is some of the sample codes.

// Form Load
private void Form1_Load(object sender, System.EventArgs e)
   // Write to richTextBox
   richTextBox1.Text = "                               " + 
   DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + 
   DateTime.Now.Year + "\r\n\r\n";
   richTextBox1.AppendText("This is a greatly simplified Print " + 
   "Document Method\r\n\r\n");
   richTextBox1.AppendText("We can write text to a richTextBox, " + 
   "or use Append Text, " + "\r\n" + "or Concatenate a String, and " + 
   "write that textBox. The " + "\r\n" + "richTextBox does not " + 
   "even have to be visible. " + "\r\n\r\n" + "Because we use a " + 
   "richTextBox it's physical dimensions are " + "\r\n" +
   "irrelevant. We can place it anywhere on our form, and set the " + 
   "\r\n" + "Visible Property to false.\r\n\r\n");
   richTextBox1.AppendText("This is the document we will print. The " + 
   "rich TextBox serves " + "\r\n" + "as a Cache for our Report, " +
   "or any other text we wish to print.\r\n\r\n");
   richTextBox1.AppendText("I have also included Print Setup " +
   "and Print Preview. ");
// Print Event
private void miPrint_Click(object sender, System.EventArgs e)
    if (printDialog1.ShowDialog() == DialogResult.OK)
// OnBeginPrint 
private void OnBeginPrint(object sender, 
                  System.Drawing.Printing.PrintEventArgs e)
    char[] param = {'\n'};

    if (printDialog1.PrinterSettings.PrintRange == PrintRange.Selection)
         lines = richTextBox1.SelectedText.Split(param);
         lines = richTextBox1.Text.Split(param);
    int i = 0;
    char[] trimParam = {'\r'};
    foreach (string s in lines)
         lines[i++] = s.TrimEnd(trimParam);
// OnPrintPage
private void OnPrintPage(object sender, 
                           System.Drawing.Printing.PrintPageEventArgs e)
    int x = e.MarginBounds.Left;
    int y = e.MarginBounds.Top;
    Brush brush = new SolidBrush(richTextBox1.ForeColor);
    while (linesPrinted < lines.Length)
    e.Graphics.DrawString (lines[linesPrinted++],
         richTextBox1.Font, brush, x, y);
    y += 15;
    if (y >= e.MarginBounds.Bottom)
         e.HasMorePages = true;
         e.HasMorePages = false;
// Page Setup
private void miSetup_Click(object sender, System.EventArgs e)
     // Call Dialog Box
// Print Preview
private void miPreview_Click(object sender, System.EventArgs e)
     // Call Dialog Box

Font and Color Dialog Boxes

When I am printing a file, or writing a report, I generally use the RichTextBox properties to set the Font Size and Style at design time. I leave the fontDialogBox, and the colorDialogBox for Windows Graphics. The fontDialogBox does not update the printPreviewDialogBox in .NET version 7.0, but then there are later versions available.


This is an easy approach to printing in .NET. Printing files, or even reports, is a breeze, and is an enjoyable task.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Dave Brighton
Web Developer
United States United States
I studied Fortran IV in HighSchool where we had 2 keypunch machines, and access to an IBM 1100 at the Community College. We ran our programs batch, and compiled our programs on paper tape.

Years later when PC's became affordable, I gave programming another shot. This time I studied RPG with the IBM AS-400 computer. I could access the College Computer with Emulator Software( Mocha Soft MW 5250 ) and my home PC.

C++ came later, then VB-6, C#.Net, and Managed C++. I am currently studying VB.Net

You may also be interested in...

Comments and Discussions

General[My vote of 2] Half-baked Pin
warnerja24-Oct-14 11:21
memberwarnerja24-Oct-14 11:21 
GeneralMy vote of 2 Pin
Member 97945766-Jun-14 6:01
memberMember 97945766-Jun-14 6:01 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 21:36
membermanoj kumar choubey26-Feb-12 21:36 
BugTypo error Pin
Macumbero7-Nov-11 5:27
memberMacumbero7-Nov-11 5:27 
QuestionFont Size Pin
BW6426-Sep-11 3:30
memberBW6426-Sep-11 3:30 
GeneralMy vote of 2 Pin
FilippoCSM9-Jan-11 14:59
memberFilippoCSM9-Jan-11 14:59 
Generalsource Code For All Pin
Aliroosta200813-Apr-10 12:32
memberAliroosta200813-Apr-10 12:32 
GeneralThank You! Pin
A.Maru3-Feb-10 2:31
memberA.Maru3-Feb-10 2:31 
GeneralLong lines Pin
Johnny J.13-Sep-09 22:22
memberJohnny J.13-Sep-09 22:22 
Generalimage in RTF Pin
laziale14-Sep-08 22:37
memberlaziale14-Sep-08 22:37 
GeneralRe: image in RTF Pin
Ant210017-Jul-11 12:56
memberAnt210017-Jul-11 12:56 
Generala similar project Pin
Benetz11-Sep-08 2:56
memberBenetz11-Sep-08 2:56 
GeneralDraw a line Pin
ctrell39-Jul-08 10:46
memberctrell39-Jul-08 10:46 
Generalprinting problem Pin
Neetu Maheshwari19-Mar-08 20:55
memberNeetu Maheshwari19-Mar-08 20:55 
Generaljustification of text in rectangle while printing.... Pin
kiran gaaaaaaaaaaa10-Feb-08 22:36
memberkiran gaaaaaaaaaaa10-Feb-08 22:36 
GeneralRe: justification of text in rectangle while printing.... Pin
vinod minhas17-Feb-08 6:51
membervinod minhas17-Feb-08 6:51 
GeneralHelp! Richtextbox giving 'System.NullReferenceException' Pin
dvo_198826-Sep-07 8:20
memberdvo_198826-Sep-07 8:20 
GeneralRe: Help! Richtextbox giving 'System.NullReferenceException' Pin
Portatofe2-Oct-08 8:34
memberPortatofe2-Oct-08 8:34 
GeneralWant to use Dot Matrix printer Pin
Abdul Basir21-Jul-07 0:26
memberAbdul Basir21-Jul-07 0:26 
GeneralPlease update the sample and put necessary files Pin
winheart14-Mar-07 2:29
memberwinheart14-Mar-07 2:29 
GeneralPrintig with C# Pin
gjuanzz22-Dec-06 4:43
membergjuanzz22-Dec-06 4:43 
QuestionDoes it print all lines? Pin
CharuT25-Jan-07 16:13
memberCharuT25-Jan-07 16:13 
AnswerRe: Does it print all lines? Pin
gjuanzz6-Feb-07 6:39
membergjuanzz6-Feb-07 6:39 
GeneralRe: Does it print all lines? Pin
CodeWatch27-Feb-07 20:59
memberCodeWatch27-Feb-07 20:59 
Generalprinting data in a richtextbox Pin
shobinmathew5-May-06 9:23
membershobinmathew5-May-06 9:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151002.1 | Last Updated 12 Jan 2005
Article Copyright 2005 by Dave Brighton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid