Click here to Skip to main content
15,867,568 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 264.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

 
Questionurgent Pin
Member 136824149-Jul-19 0:33
Member 136824149-Jul-19 0:33 
Questionhow to set paper size to the printer Pin
briliyant_vinit18-Jun-19 3:35
briliyant_vinit18-Jun-19 3:35 
QuestionLoad/Eject work Pin
jafartjafar23-Nov-17 23:09
jafartjafar23-Nov-17 23:09 
Questionstart printer after last page is spooled not working Pin
Althaf Alfazin1-Oct-17 20:02
Althaf Alfazin1-Oct-17 20:02 
Questionis working in usb cable? Pin
Sarath Chand27-Dec-16 23:35
Sarath Chand27-Dec-16 23:35 
QuestionI canot set command ESC on OKI 320 Turbo Pin
Pepe Luna4-May-16 16:53
Pepe Luna4-May-16 16:53 
AnswerRe: I canot set command ESC on OKI 320 Turbo Pin
Antonino Porcino5-May-16 1:34
Antonino Porcino5-May-16 1:34 
QuestionGreat Class! Problem with document disappearing. Pin
wkiess0113-Dec-15 17:39
wkiess0113-Dec-15 17:39 
AnswerRe: Great Class! Problem with document disappearing. Pin
Antonino Porcino13-Dec-15 21:28
Antonino Porcino13-Dec-15 21:28 
Suggestionstring length mistake Pin
coder_bug10-Aug-15 7:11
coder_bug10-Aug-15 7:11 
GeneralRe: string length mistake Pin
Antonino Porcino10-Aug-15 7:18
Antonino Porcino10-Aug-15 7:18 
Questionhow to send keys Pin
Althaf Alfazin26-May-15 18:20
Althaf Alfazin26-May-15 18:20 
AnswerRe: how to send keys Pin
Antonino Porcino26-May-15 21:44
Antonino Porcino26-May-15 21:44 
QuestionHow to Print a Table or Grid using this code Pin
Althaf Alfazin25-May-15 3:51
Althaf Alfazin25-May-15 3:51 
AnswerRe: How to Print a Table or Grid using this code Pin
Antonino Porcino25-May-15 4:37
Antonino Porcino25-May-15 4:37 
there's nothing specific to print a table, just use string.Format() to prepare a line and then print each row one by one. This is what we used to do in the old days.

For example:
C#
//               1234567890123456789021234567812345
myprinter.Print("Description          Price     Qty\r\n");
myprinter.Print("----------------------------------\r\n");
foreach(var row in mytable)
{
  var line = string.Format("{0,-32} {1,8:##0.00} {2,5}\r\n",row.ItemDesc, row.Price, row.Qty);
  myprinter.Print(line);
}

GeneralRe: How to Print a Table or Grid using this code Pin
Althaf Alfazin25-May-15 19:47
Althaf Alfazin25-May-15 19:47 
GeneralRe: How to Print a Table or Grid using this code Pin
Antonino Porcino25-May-15 21:39
Antonino Porcino25-May-15 21:39 
GeneralRe: How to Print a Table or Grid using this code Pin
Althaf Alfazin26-May-15 1:27
Althaf Alfazin26-May-15 1:27 
GeneralPrint to Dot Matrix Printer in ASP.NET C# Pin
Lakhpat-Singh12-Jan-15 0:57
Lakhpat-Singh12-Jan-15 0:57 
GeneralRe: Print to Dot Matrix Printer in ASP.NET C# Pin
Antonino Porcino12-Jan-15 7:00
Antonino Porcino12-Jan-15 7:00 
Questionhow to change bold Pin
Member 1125869823-Nov-14 22:24
Member 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
Antonino 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
Nithin satheesh15-Jun-14 18: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.