Click here to Skip to main content
11,572,389 members (58,789 online)
Click here to Skip to main content

Line Printer Class in C#

, 26 Sep 2008 Public Domain 103.6K 4.8K 53
Rate this:
Please Sign up or sign in to vote.
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:

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 neeeded)
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 codes, 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

You may also be interested in...

Comments and Discussions

 
Questionhow to send keys Pin
Member 1028142226-May-15 18:20
memberMember 1028142226-May-15 18:20 
AnswerRe: how to send keys Pin
Antonino Porcino26-May-15 21:44
memberAntonino Porcino26-May-15 21:44 
QuestionHow to Print a Table or Grid using this code Pin
Member 1028142225-May-15 3:51
memberMember 1028142225-May-15 3:51 
AnswerRe: How to Print a Table or Grid using this code Pin
Antonino Porcino25-May-15 4:37
memberAntonino Porcino25-May-15 4:37 
GeneralRe: How to Print a Table or Grid using this code Pin
Member 1028142225-May-15 19:47
memberMember 1028142225-May-15 19:47 
GeneralRe: How to Print a Table or Grid using this code Pin
Antonino Porcino25-May-15 21:39
memberAntonino Porcino25-May-15 21:39 
GeneralRe: How to Print a Table or Grid using this code Pin
Member 1028142226-May-15 1:27
memberMember 1028142226-May-15 1:27 
GeneralPrint to Dot Matrix Printer in ASP.NET C# Pin
Lakhpat-Singh12-Jan-15 0:57
memberLakhpat-Singh12-Jan-15 0:57 
GeneralRe: Print to Dot Matrix Printer in ASP.NET C# Pin
Antonino Porcino12-Jan-15 7:00
memberAntonino Porcino12-Jan-15 7:00 
Questionhow to change bold Pin
Member 1125869823-Nov-14 22:24
memberMember 1125869823-Nov-14 22:24 
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
Azeem.17-Feb-14 10:46
memberAzeem.17-Feb-14 10:46 
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 
GeneralRe: Arabic Fonts doesn't work correctly Pin
M. Mokhtar3-May-13 10:54
memberM. Mokhtar3-May-13 10:54 
GeneralRe: Arabic Fonts doesn't work correctly Pin
Antonino Porcino3-May-13 21:57
memberAntonino Porcino3-May-13 21:57 
GeneralThanks for posting Pin
Petar Srdanovic15-Mar-13 0:23
memberPetar Srdanovic15-Mar-13 0:23 
QuestionBill Printing Issue Pin
Kamran Ismail3-Mar-13 19:06
memberKamran Ismail3-Mar-13 19:06 
AnswerRe: Bill Printing Issue Pin
Antonino Porcino3-Mar-13 21:28
memberAntonino Porcino3-Mar-13 21:28 
QuestionNot working in VB.NET Pin
Peter Yulius10-Jan-13 3:37
memberPeter Yulius10-Jan-13 3:37 
AnswerRe: Not working in VB.NET Pin
Antonino Porcino10-Jan-13 6:48
memberAntonino Porcino10-Jan-13 6:48 
GeneralMy vote of 5 Pin
wkiess0115-Jan-12 15:37
memberwkiess0115-Jan-12 15:37 
QuestionMake gujrati print in C# Pin
CHAVADA SURAJ12-Dec-11 15:03
memberCHAVADA SURAJ12-Dec-11 15:03 
AnswerRe: Make gujrati print in C# Pin
Antonino Porcino12-Dec-11 21:36
memberAntonino Porcino12-Dec-11 21:36 
AnswerRe: Make gujrati print in C# Pin
lukac23-Dec-11 0:39
memberlukac23-Dec-11 0:39 
QuestionLine Printer Class problem Pin
Zil-e-Huma16-Aug-11 1:03
memberZil-e-Huma16-Aug-11 1:03 
AnswerRe: Line Printer Class problem Pin
Antonino Porcino16-Aug-11 3:55
memberAntonino Porcino16-Aug-11 3:55 
GeneralRe: Line Printer Class problem Pin
Zil-e-Huma16-Aug-11 17:50
memberZil-e-Huma16-Aug-11 17:50 
GeneralRe: Line Printer Class problem Pin
Zil-e-Huma16-Aug-11 19:42
memberZil-e-Huma16-Aug-11 19:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 26 Sep 2008
Article Copyright 2008 by Antonino Porcino
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid