Click here to Skip to main content
15,886,069 members
Articles / Desktop Programming / Windows Forms
Article

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 94.5K   1.8K   30   15
A TextWriter object for writing to a printer

Introduction

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.

Background

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
ELSE IF Where$="Printer" THEN OPEN "LPT1:" FOR OUTPUT AS 3
ELSE OPEN "CONS:" 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...

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

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

C#
TextWriter output = TextWriter.Null;
if (Where == "File")
    output = new StreamWriter("output.txt");
else 
    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):

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

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:

C#
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:

C#
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:

C#
HtmlAnchor A = new HtmlAnchor();
A.HRef = "http://www.msn.com";
A.InnerText = "MSN";
A.RenderControl(new HtmlTextWriter(lprint));

Returning to our switchable output example:

C#
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).

Implementation

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.

History

  • 8th June, 2006 - Initial release

License

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


Written By
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 www.NJTheater.com as a hobby.

Full resume & stuff at NovelTheory.com

Underused blog at HonestIllusion.com

Comments and Discussions

 
GeneralThe wizard behind the curtain.. Pin
craigg7525-Aug-08 7:17
craigg7525-Aug-08 7:17 
GeneralRe: The wizard behind the curtain.. Pin
James Curran25-Aug-08 13:18
James Curran25-Aug-08 13:18 
GeneralWorks great - and a question [modified] Pin
Nina987655-Dec-06 7:26
Nina987655-Dec-06 7:26 
GeneralRe: Works great - and a question Pin
James Curran6-Dec-06 1:47
James Curran6-Dec-06 1:47 
GeneralRe: Works great - and a question Pin
Nina987656-Dec-06 6:18
Nina987656-Dec-06 6:18 
QuestionLooks great but how do i... Pin
GaZbI14-Jun-06 2:15
GaZbI14-Jun-06 2:15 
AnswerRe: Looks great but how do i... Pin
James Curran14-Jun-06 9:18
James Curran14-Jun-06 9:18 
GeneralRe: Looks great but how do i... Pin
GaZbI15-Jun-06 4:34
GaZbI15-Jun-06 4:34 
GeneralRe: Looks great but how do i... Pin
Member 135347211-Jun-08 17:37
Member 135347211-Jun-08 17:37 
GeneralExcellant Pin
rippo12-Jun-06 20:50
rippo12-Jun-06 20:50 
Good article, I too used to use LPrint for printing to flower pot labels directly to a dot matrix printer!
Has anyone ever had experience in extending this so you can print PDFS directly to the printer?
GeneralRe: Excellant Pin
James Curran13-Jun-06 2:00
James Curran13-Jun-06 2:00 
QuestionRe: Excellant Pin
MarkSchultz20-Nov-06 9:04
MarkSchultz20-Nov-06 9:04 
GeneralGreat LPrint is back Pin
ruben ruvalcaba12-Jun-06 14:54
ruben ruvalcaba12-Jun-06 14:54 
GeneralRe: Great LPrint is back Pin
James Curran12-Jun-06 16:42
James Curran12-Jun-06 16:42 
QuestionRe: Great LPrint is back Pin
MarkSchultz20-Nov-06 9:06
MarkSchultz20-Nov-06 9: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.