Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / Win32

Line Printer Class in C#

Rate me:
Please Sign up or sign in to vote.
4.70/5 (34 votes)
26 Sep 2008Public Domain1 min read 266.2K   8.7K   64   115
A simple C# class for printing on old dot matrix line printers

Introduction

This class simplifies the job of printing text on line printers, in C#. I had an application that needed to print on one of such printers, so I ended up writing this helper class. Basically, it's a wrapper for the spooler API (winspool.drv); the text to be printed is sent in "RAW" mode to the spooler so that no graphics printing is involved.

Using the Code

The use of the class is pretty easy. First, the desired printing device is selected by either writing the PrinterName property or by calling the ChoosePrinter() method:

C#
LPrinter MyPrinter = new LPrinter(); // creates the printer object
MyPrinter.ChoosePrinter();// let user choose the printer device

// alternatively:
MyPrinter.PrinterName = "Epson FX-80";
// uses a specific named printer

The actual printing is done like this:

C#
MyPrinter.Open("Document Title Here"); // opens and tells the spooler the document title
MyPrinter.Print("Some text....\r\n");  // actual printing (CR+LFs are needed)
MyPrinter.Print("\x0C");               // sends a form feed (ejects the paper)
MyPrinter.Close();                     // Close the spooler

All methods Open(), Print(), Close(), and ChoosePrinter() return a boolean, in case you want to check if the operation was successful.

Some basic text effects can be achieved by sending control codes to the printer, according to the following table:

Code Description
14 enlarged characters on
20 enlarged characters off
15 compressed characters on
18 compressed characters off
ESC + "E" bold characters on
ESC + "F" bold characters off
12 FF (form feed)

For more code, check the printer's manual.

Points of Interest

Unfortunately, there is no easy way to tell if an installed printer is a line printer or a graphical one (laser or inkjet), so I was not able to filter the print dialog of ChoosePrinter(), making it display only line printers. This means that the user has to choose the right printer.

History

  • 29-Sep-2008: First (and possibly only one) version

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Tear Off & Reverse line feed Pin
Member 384713429-Jul-09 0:00
Member 384713429-Jul-09 0:00 
GeneralRe: Tear Off & Reverse line feed Pin
Antonino Porcino29-Jul-09 0:45
Antonino Porcino29-Jul-09 0:45 
QuestionLandscape? Pin
Mrestivo4-Jun-09 4:59
Mrestivo4-Jun-09 4:59 
GeneralIdea to extend the class Pin
rob tillaart29-Sep-08 19:50
rob tillaart29-Sep-08 19:50 
Generalprint in bold... Pin
TheCardinal26-Sep-08 18:29
TheCardinal26-Sep-08 18:29 
GeneralRe: print in bold... Pin
Antonino Porcino26-Sep-08 22:02
Antonino Porcino26-Sep-08 22:02 
GeneralRe: print in bold... Pin
TheCardinal27-Sep-08 1:11
TheCardinal27-Sep-08 1:11 
GeneralRe: print in bold and others.. Pin
Geofrey Burgos1-Dec-08 5:51
Geofrey Burgos1-Dec-08 5:51 
GeneralRe: print in bold... Pin
mahendiali12-Sep-13 0:33
mahendiali12-Sep-13 0:33 
GeneralRe: print in bold... Pin
Antonino Porcino12-Sep-13 2:39
Antonino Porcino12-Sep-13 2:39 
GeneralRe: print in bold... Pin
mahendiali29-Sep-13 18:44
mahendiali29-Sep-13 18:44 
GeneralRe: print in bold... Pin
Antonino Porcino29-Sep-13 20:52
Antonino Porcino29-Sep-13 20:52 
Generalthat remind me the old days Pin
Huisheng Chen26-Sep-08 16:32
Huisheng Chen26-Sep-08 16:32 
GeneralRe: that remind me the old days Pin
Richard Grenfell6-Oct-08 2:31
Richard Grenfell6-Oct-08 2:31 

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.