65.9K
CodeProject is changing. Read more.
Home

Writing Text to a Printer with LPrintWriter

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.18/5 (9 votes)

Jun 8, 2006

CPOL

4 min read

viewsIcon

95922

downloadIcon

1809

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...

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");
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):

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:

 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 = "http://www.msn.com";
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).

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