Click here to Skip to main content
14,392,678 members

Line Printer Class in C#

Rate this:
4.70 (34 votes)
Please Sign up or sign in to vote.
4.70 (34 votes)
26 Sep 2008Public Domain
A simple C# class for printing on old dot matrix line printers


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:

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:

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.


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


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


About the Author

Antonino Porcino
Software Developer
Italy Italy
No Biography provided

Comments and Discussions

Questionurgent Pin
Member 136824149-Jul-19 1:33
MemberMember 136824149-Jul-19 1:33 
Questionhow to set paper size to the printer Pin
briliyant_vinit18-Jun-19 4:35
Memberbriliyant_vinit18-Jun-19 4:35 
QuestionLoad/Eject work Pin
jafartjafar24-Nov-17 0:09
Memberjafartjafar24-Nov-17 0:09 
Questionstart printer after last page is spooled not working Pin
Althaf Alfazin1-Oct-17 21:02
MemberAlthaf Alfazin1-Oct-17 21:02 
Questionis working in usb cable? Pin
Sarath Chand28-Dec-16 0:35
MemberSarath Chand28-Dec-16 0:35 
QuestionI canot set command ESC on OKI 320 Turbo Pin
Pepe Luna4-May-16 17:53
MemberPepe Luna4-May-16 17:53 
AnswerRe: I canot set command ESC on OKI 320 Turbo Pin
Antonino Porcino5-May-16 2:34
MemberAntonino Porcino5-May-16 2:34 
QuestionGreat Class! Problem with document disappearing. Pin
wkiess0113-Dec-15 18:39
Memberwkiess0113-Dec-15 18:39 
AnswerRe: Great Class! Problem with document disappearing. Pin
Antonino Porcino13-Dec-15 22:28
MemberAntonino Porcino13-Dec-15 22:28 
Suggestionstring length mistake Pin
abcayad10-Aug-15 8:11
Memberabcayad10-Aug-15 8:11 
GeneralRe: string length mistake Pin
Antonino Porcino10-Aug-15 8:18
MemberAntonino Porcino10-Aug-15 8:18 
Questionhow to send keys Pin
Althaf Alfazin26-May-15 19:20
MemberAlthaf Alfazin26-May-15 19:20 
AnswerRe: how to send keys Pin
Antonino Porcino26-May-15 22:44
MemberAntonino Porcino26-May-15 22:44 
QuestionHow to Print a Table or Grid using this code Pin
Althaf Alfazin25-May-15 4:51
MemberAlthaf Alfazin25-May-15 4:51 
AnswerRe: How to Print a Table or Grid using this code Pin
Antonino Porcino25-May-15 5:37
MemberAntonino Porcino25-May-15 5:37 
GeneralRe: How to Print a Table or Grid using this code Pin
Althaf Alfazin25-May-15 20:47
MemberAlthaf Alfazin25-May-15 20:47 
GeneralRe: How to Print a Table or Grid using this code Pin
Antonino Porcino25-May-15 22:39
MemberAntonino Porcino25-May-15 22:39 
GeneralRe: How to Print a Table or Grid using this code Pin
Althaf Alfazin26-May-15 2:27
MemberAlthaf Alfazin26-May-15 2:27 
GeneralPrint to Dot Matrix Printer in ASP.NET C# Pin
Lakhpat-Singh12-Jan-15 1:57
MemberLakhpat-Singh12-Jan-15 1:57 
GeneralRe: Print to Dot Matrix Printer in ASP.NET C# Pin
Antonino Porcino12-Jan-15 8:00
MemberAntonino Porcino12-Jan-15 8:00 
Questionhow to change bold Pin
Member 1125869823-Nov-14 23:24
MemberMember 1125869823-Nov-14 23:24 
Questionsetting printer to line zero Pin
Meher Khan18-Jun-14 1:57
professionalMeher Khan18-Jun-14 1:57 
AnswerRe: setting printer to line zero Pin
Antonino Porcino18-Jun-14 4:25
MemberAntonino Porcino18-Jun-14 4:25 
GeneralRe: setting printer to line zero Pin
Meher Khan18-Jun-14 5:33
professionalMeher Khan18-Jun-14 5:33 
QuestionFont Size Pin
Nithin satheesh15-Jun-14 19:53
MemberNithin satheesh15-Jun-14 19:53 

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.

Posted 26 Sep 2008

Tagged as


60 bookmarked