Click here to Skip to main content
15,073,499 members
Articles / Desktop Programming / Win32
Article
Posted 26 Sep 2008

Tagged as

Stats

231K views
8.4K downloads
62 bookmarked

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

Share

About the Author

Antonino Porcino
Software Developer
Italy Italy
No Biography provided

Comments and Discussions

 
Questionsetting printer to line zero Pin
Meher Khan18-Jun-14 0:57
professionalMeher Khan18-Jun-14 0:57 
AnswerRe: setting printer to line zero Pin
Antonino Porcino18-Jun-14 3:25
MemberAntonino Porcino18-Jun-14 3:25 
GeneralRe: setting printer to line zero Pin
Meher Khan18-Jun-14 4:33
professionalMeher Khan18-Jun-14 4:33 
QuestionFont Size Pin
Nithin satheesh15-Jun-14 18:53
MemberNithin satheesh15-Jun-14 18:53 
AnswerRe: Font Size Pin
Antonino Porcino16-Jun-14 0:46
MemberAntonino Porcino16-Jun-14 0:46 
QuestionPrint direcly to default printer without dialog printer Pin
k-926-Mar-14 8:01
Memberk-926-Mar-14 8:01 
AnswerRe: Print direcly to default printer without dialog printer Pin
Antonino Porcino26-Mar-14 9:17
MemberAntonino Porcino26-Mar-14 9:17 
QuestionHow to print image i.e Company logo using this API ????? Pin
DoingWork17-Feb-14 10:46
MemberDoingWork17-Feb-14 10:46 
How to print image i.e Company logo using this API ?????
AnswerRe: How to print image i.e Company logo using this API ????? Pin
Antonino Porcino17-Feb-14 21:09
MemberAntonino Porcino17-Feb-14 21:09 
Questionchange font size Pin
k-924-Jan-14 22:03
Memberk-924-Jan-14 22:03 
AnswerRe: change font size Pin
Antonino Porcino24-Jan-14 22:15
MemberAntonino Porcino24-Jan-14 22:15 
GeneralRe: change font size Pin
k-924-Jan-14 23:06
Memberk-924-Jan-14 23:06 
GeneralRe: change font size Pin
Antonino Porcino24-Jan-14 23:22
MemberAntonino Porcino24-Jan-14 23:22 
GeneralRe: change font size Pin
k-924-Jan-14 23:34
Memberk-924-Jan-14 23:34 
GeneralRe: change font size Pin
k-91-Feb-14 4:11
Memberk-91-Feb-14 4:11 
QuestionClass to print on any windows printer Pin
eyanson13-Jun-13 15:21
Membereyanson13-Jun-13 15:21 
AnswerRe: Class to print on any windows printer Pin
Antonino Porcino13-Jun-13 21:33
MemberAntonino Porcino13-Jun-13 21:33 
QuestionFor Virtual Printer like Bullzip PDF Printer Pin
Member 901324221-May-13 11:28
MemberMember 901324221-May-13 11:28 
AnswerRe: For Virtual Printer like Bullzip PDF Printer Pin
Antonino Porcino21-May-13 21:01
MemberAntonino Porcino21-May-13 21:01 
GeneralRe: For Virtual Printer like Bullzip PDF Printer Pin
Member 901324222-May-13 6:44
MemberMember 901324222-May-13 6:44 
QuestionArabic Fonts doesn't work correctly Pin
M. Mokhtar28-Apr-13 7:47
MemberM. Mokhtar28-Apr-13 7:47 
AnswerRe: Arabic Fonts doesn't work correctly Pin
Antonino Porcino28-Apr-13 8:49
MemberAntonino Porcino28-Apr-13 8:49 
GeneralRe: Arabic Fonts doesn't work correctly Pin
M. Mokhtar28-Apr-13 8:57
MemberM. Mokhtar28-Apr-13 8:57 
GeneralRe: Arabic Fonts doesn't work correctly Pin
Antonino Porcino28-Apr-13 21:05
MemberAntonino Porcino28-Apr-13 21:05 
GeneralRe: Arabic Fonts doesn't work correctly Pin
M. Mokhtar3-May-13 10:57
MemberM. Mokhtar3-May-13 10:57 

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.