Click here to Skip to main content
15,172,951 members
Articles / Desktop Programming / Windows Forms
Posted 8 Jun 2006


30 bookmarked

Writing Text to a Printer with LPrintWriter

Rate me:
Please Sign up or sign in to vote.
4.18/5 (9 votes)
8 Jun 2006CPOL4 min read
A TextWriter object for writing to a printer


Writing plain text to the screen is as simple as Console.WriteLine("{0}", myObj); Writing plain text out to a printer should be as well.


Many of us learned programming with GW-Basic in MSDOS on an IBM PC (Actually, for me it was Level II Basic in TRSDOS on the TRS-80). Back then, we didn't have the fancy GUI displays we have now. Printing to the screen was simple (primitive) but simple (easy). One just had to write...

PRINT "Hello, World!"

... and the text was printed on the screen. And just as simply, if you wrote...

LPRINT "Hello, World!"

... it would be typed out on the attached printer (which was referred to as a "Line Printer", hence the LPRINT command). In fact, you could "open" the screen or printer as a pseudo-file, and direct the text to whichever at run-time:

IF Where$ = "File" THEN OPEN "Output.txt" FOR OUTPUT AS 3

PRINT#3, "Hello, World!"

Then came Windows, which made drawing full graphics on the screen and printer possible, but made writing anything to the screen or printer difficult. Now, we have .NET, which has reclaimed the simplicity of the Microsoft DOS command line in Console mode applications. Once again, it's just a simple...

Console.WriteLine("Hello, World!");

... to display text on the screen. Similarly, choosing between the screen or a text file can be handled like this:

TextWriter output = TextWriter.Null;
if (Where == "File")
    output = new StreamWriter("output.txt");
    output = Console.Out

output.WriteLine("Hello, World!");

And yet, writing to the printer is as difficult as ever. What we need to fill the hole is a TextWriter-derived class for writing to a printer. LPrintWriter is that class.

Using the Code

Use of LPrintWriter is as simple as promised (just about):

LPrintWriter lprint = new LPrintWriter();
lprint.WriteLine("Hello, world!");

Because we are still going through Windows, where printing is still page- & spool-oriented, the Close() at the end is needed to start the actual printing. When the Close is executed, the standard Windows printer dialog is displayed so you can choose your printer and printer settings.

(Actually, the dialog is displayed and printing started upon the Flush() method. Close() calls Flush(). The dialog is displayed only the first time Flush() is called. Thereafter, the same settings are used. To change the setting, you'd have to create a new LPrintWriter object.)

LPrintWriter will default to printing the text in the 10pt Courier New typeface -- monospaced to match output to the screen from Console.WriteLine. However, you can change that to any installed font -- fixed or proportional -- with the Font property:

lprint.Font = new System.Drawing.Font("Arial", 22.0f);

Similarly, you can change the color of the text (assuming your printer can handle it), using the TextColor property:

lprint.TextColor = Color.Blue;

Note that the font & color settings apply to the entire printout. You cannot set different fonts for different sections of a page.

Additionally, LPrintWriter is a properly derived subclass of TextWriter, so everything you'd expect in a TextWriter class (Flush, Dispose, and the 17 variants of Write & WriteLine) is there. LPrintWriter therefore can be used anywhere a TextWriter is called for. For example, if you wanted to produce & print out some HTML code:

HtmlAnchor A = new HtmlAnchor();
A.HRef = "";
A.InnerText = "MSN";
A.RenderControl(new HtmlTextWriter(lprint));

Returning to our switchable output example:

TextWriter output = TextWriter.Null;
if (Where == "File")          output = new StreamWriter("output.txt");
else if (Where == "Printer")       output = new LPrintWriter();
else                    output = Console.Out

output.WriteLine("Hello, World!");

Note, that, despite its purpose to be used in Console application, since it uses PrintDialog, an application which uses LPrintWriter will need a reference to the System.Windows.Forms assembly.

Finally, it is written in C#, but if placed in a class library it can be used from a VB.NET application (that was actually the purpose for which it was written).


The nice thing about TextWriter is that all its methods are written in terms of each other, so one only needs to override a minimum of methods. Basically, all you really need to do to create a derived class is code a Write(char) method. Every other Write & WriteLine method will call it (eventually). You only would have to implement other write methods if there was a particular optimization available.

However, in this case, I knew I just needed to collect the text as a big string until it was time to print it, so I derived from StringWriter (whose job is to collect text into a big string). All I had to do was override the Flush() method to handle the actual print.

Printing is pretty much straightforward. At the start, the big string is divided up into an array of little strings -- one for each line. Then for each page, the little strings are pulled from the array and sent to the print spooler until a page is filled.


  • 8th June, 2006 - Initial release


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


About the Author

James Curran
Software Developer (Senior) NovelTheory LLC
United States United States
20+ years as a developer : Assembly, C, C++ and C# (in that order) with sidelines in ASP/VBScript, ASP.Net, JavaScript, Perl, QuickBasic, VisualBasic, plus a few others which I'm not going to mention because if I did someone might ask me to use them again (shudder)

Microsoft MVP in VC++ (1994-2004)

I also run as a hobby.

Full resume & stuff at

Underused blog at

Comments and Discussions

GeneralThe wizard behind the curtain.. Pin
craigg7525-Aug-08 8:17
Membercraigg7525-Aug-08 8:17 
GeneralRe: The wizard behind the curtain.. Pin
James Curran25-Aug-08 14:18
MemberJames Curran25-Aug-08 14:18 
GeneralWorks great - and a question [modified] Pin
Nina987655-Dec-06 8:26
MemberNina987655-Dec-06 8:26 
GeneralRe: Works great - and a question Pin
James Curran6-Dec-06 2:47
MemberJames Curran6-Dec-06 2:47 
GeneralRe: Works great - and a question Pin
Nina987656-Dec-06 7:18
MemberNina987656-Dec-06 7:18 
QuestionLooks great but how do i... Pin
GaZbI14-Jun-06 3:15
MemberGaZbI14-Jun-06 3:15 
AnswerRe: Looks great but how do i... Pin
James Curran14-Jun-06 10:18
MemberJames Curran14-Jun-06 10:18 
GeneralRe: Looks great but how do i... Pin
GaZbI15-Jun-06 5:34
MemberGaZbI15-Jun-06 5:34 
GeneralRe: Looks great but how do i... Pin
Member 135347211-Jun-08 18:37
MemberMember 135347211-Jun-08 18:37 
GeneralExcellant Pin
rippo12-Jun-06 21:50
Memberrippo12-Jun-06 21:50 
GeneralRe: Excellant Pin
James Curran13-Jun-06 3:00
MemberJames Curran13-Jun-06 3:00 
QuestionRe: Excellant Pin
MarkSchultz20-Nov-06 10:04
MemberMarkSchultz20-Nov-06 10:04 
GeneralGreat LPrint is back Pin
ruben ruvalcaba12-Jun-06 15:54
Memberruben ruvalcaba12-Jun-06 15:54 
GeneralRe: Great LPrint is back Pin
James Curran12-Jun-06 17:42
MemberJames Curran12-Jun-06 17:42 
QuestionRe: Great LPrint is back Pin
MarkSchultz20-Nov-06 10:06
MemberMarkSchultz20-Nov-06 10:06 

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.