Click here to Skip to main content
Click here to Skip to main content

Line Printer Class in C#

, 26 Sep 2008
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

Comments and Discussions

 
Questionsetting printer to line zero PinprofessionalMeher Khan18-Jun-14 0:57 
AnswerRe: setting printer to line zero PinmemberAntonino Porcino18-Jun-14 3:25 
GeneralRe: setting printer to line zero PinprofessionalMeher Khan18-Jun-14 4:33 
QuestionFont Size PinmemberNithin satheesh15-Jun-14 18:53 
AnswerRe: Font Size PinmemberAntonino Porcino16-Jun-14 0:46 
QuestionPrint direcly to default printer without dialog printer Pinmemberk-926-Mar-14 8:01 
AnswerRe: Print direcly to default printer without dialog printer PinmemberAntonino Porcino26-Mar-14 9:17 
QuestionHow to print image i.e Company logo using this API ????? PinmemberAzeem.17-Feb-14 10:46 
AnswerRe: How to print image i.e Company logo using this API ????? PinmemberAntonino Porcino17-Feb-14 21:09 
Questionchange font size Pinmemberk-924-Jan-14 22:03 
AnswerRe: change font size PinmemberAntonino Porcino24-Jan-14 22:15 
GeneralRe: change font size Pinmemberk-924-Jan-14 23:06 
GeneralRe: change font size PinmemberAntonino Porcino24-Jan-14 23:22 
GeneralRe: change font size Pinmemberk-924-Jan-14 23:34 
GeneralRe: change font size Pinmemberk-91-Feb-14 4:11 
QuestionClass to print on any windows printer Pinmembereyanson13-Jun-13 15:21 
AnswerRe: Class to print on any windows printer PinmemberAntonino Porcino13-Jun-13 21:33 
QuestionFor Virtual Printer like Bullzip PDF Printer PinmemberMember 901324221-May-13 11:28 
AnswerRe: For Virtual Printer like Bullzip PDF Printer PinmemberAntonino Porcino21-May-13 21:01 
GeneralRe: For Virtual Printer like Bullzip PDF Printer PinmemberMember 901324222-May-13 6:44 
QuestionArabic Fonts doesn't work correctly PinmemberM. Mokhtar28-Apr-13 7:47 
When printing Arabic fonts it doesn't work correctly?
AnswerRe: Arabic Fonts doesn't work correctly PinmemberAntonino Porcino28-Apr-13 8:49 
GeneralRe: Arabic Fonts doesn't work correctly PinmemberM. Mokhtar28-Apr-13 8:57 
GeneralRe: Arabic Fonts doesn't work correctly PinmemberAntonino Porcino28-Apr-13 21:05 
GeneralRe: Arabic Fonts doesn't work correctly PinmemberM. Mokhtar3-May-13 10:57 
GeneralRe: Arabic Fonts doesn't work correctly PinmemberM. Mokhtar3-May-13 10:54 
GeneralRe: Arabic Fonts doesn't work correctly PinmemberAntonino Porcino3-May-13 21:57 
GeneralThanks for posting PinmemberPetar Srdanovic15-Mar-13 0:23 
QuestionBill Printing Issue PinmemberKamran Ismail3-Mar-13 19:06 
AnswerRe: Bill Printing Issue PinmemberAntonino Porcino3-Mar-13 21:28 
QuestionNot working in VB.NET PinmemberPeter Yulius10-Jan-13 3:37 
AnswerRe: Not working in VB.NET PinmemberAntonino Porcino10-Jan-13 6:48 
GeneralMy vote of 5 Pinmemberwkiess0115-Jan-12 15:37 
QuestionMake gujrati print in C# PinmemberCHAVADA SURAJ12-Dec-11 15:03 
AnswerRe: Make gujrati print in C# PinmemberAntonino Porcino12-Dec-11 21:36 
AnswerRe: Make gujrati print in C# Pinmemberlukac23-Dec-11 0:39 
QuestionLine Printer Class problem PinmemberZil-e-Huma16-Aug-11 1:03 
AnswerRe: Line Printer Class problem PinmemberAntonino Porcino16-Aug-11 3:55 
GeneralRe: Line Printer Class problem PinmemberZil-e-Huma16-Aug-11 17:50 
GeneralRe: Line Printer Class problem PinmemberZil-e-Huma16-Aug-11 19:42 
GeneralRe: Line Printer Class problem PinmemberAntonino Porcino16-Aug-11 20:57 
GeneralRe: Line Printer Class problem PinmemberZil-e-Huma17-Aug-11 0:06 
GeneralRe: Line Printer Class problem PinmemberAntonino Porcino17-Aug-11 1:50 
GeneralRe: Line Printer Class problem PinmemberZil-e-Huma17-Aug-11 4:56 
GeneralRe: Line Printer Class problem PinmemberAntonino Porcino17-Aug-11 5:32 
GeneralRe: Line Printer Class problem PinmemberZil-e-Huma17-Aug-11 18:18 
GeneralRe: Line Printer Class problem PinmemberAntonino Porcino17-Aug-11 22:26 
GeneralRe: Line Printer Class problem PinmemberZil-e-Huma17-Aug-11 23:04 
GeneralRe: Line Printer Class problem PinmemberZil-e-Huma17-Aug-11 23:32 
GeneralRe: Line Printer Class problem PinmemberAntonino Porcino18-Aug-11 0:09 

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 | Mobile
Web01 | 2.8.140827.1 | Last Updated 26 Sep 2008
Article Copyright 2008 by Antonino Porcino
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid