Click here to Skip to main content
15,891,864 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 267.5K   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

 
AnswerRe: Arabic Fonts doesn't work correctly Pin
Antonino Porcino28-Apr-13 8:49
Antonino Porcino28-Apr-13 8:49 
GeneralRe: Arabic Fonts doesn't work correctly Pin
M. Mokhtar28-Apr-13 8:57
M. Mokhtar28-Apr-13 8:57 
GeneralRe: Arabic Fonts doesn't work correctly Pin
Antonino Porcino28-Apr-13 21:05
Antonino Porcino28-Apr-13 21:05 
GeneralRe: Arabic Fonts doesn't work correctly Pin
M. Mokhtar3-May-13 10:57
M. Mokhtar3-May-13 10:57 
GeneralRe: Arabic Fonts doesn't work correctly Pin
M. Mokhtar3-May-13 10:54
M. Mokhtar3-May-13 10:54 
GeneralRe: Arabic Fonts doesn't work correctly Pin
Antonino Porcino3-May-13 21:57
Antonino Porcino3-May-13 21:57 
GeneralThanks for posting Pin
Petar Srdanovic15-Mar-13 0:23
Petar Srdanovic15-Mar-13 0:23 
QuestionBill Printing Issue Pin
Kamran Ismail3-Mar-13 19:06
Kamran Ismail3-Mar-13 19:06 
I am printing bill with thermal printer. But it only print with Raw Data. I am facing two problems in below code.
C#
if (dataGrid_product_list.RowCount > 1)
            {

                MyPrinter.PrinterName = "Epson LX-300+";
                    if (!MyPrinter.Open("Test Page")) return;
                    MyPrinter.Print("             ZIA BOOK SELLER\r\n");
                    MyPrinter.Print("             D-Ground FSD\r\n");
                    //MessageBox.Show("OK");
                    MyPrinter.Print("      Ph. 041-8732670, 041-8540217\r\n");
                    MyPrinter.Print("Date: " + CurrentDate + "\r\n");
                    MyPrinter.Print("Invoice No.:" + MAX + "\r\n");
                    MyPrinter.Print("----------------------------------------\r\n");
                    MyPrinter.Print(" Sr. | Product Name    | Price  | Qty |\r\n");
                    MyPrinter.Print("----------------------------------------\r\n");
                    double sum = 0;
                    string name = "";
                    double sr = 1;
                    double price = 0;
                    double amount = 0;
                    double qty = 0;
                    for (int i = 0; i < dataGrid_product_list.Rows.Count; ++i)
                    {
                        sr += 1;
                       

                        name = Convert.ToString(dataGrid_product_list.Rows[i].Cells[1].Value);
                        price = Convert.ToDouble(dataGrid_product_list.Rows[i].Cells[2].Value);
                        qty = Convert.ToDouble(dataGrid_product_list.Rows[i].Cells[3].Value);
                        amount = Convert.ToDouble(dataGrid_product_list.Rows[i].Cells[4].Value);
                        MyPrinter.Print(" " + sr.ToString() + "  " + Name.ToString() + " " + price.ToString() + " " + qty.ToString() + " " + amount.ToString() + "\r\n");

                        sum += Convert.ToDouble(dataGrid_product_list.Rows[i].Cells[4].Value);

                    }
                    MyPrinter.Print("----------------------------------------\r\n");
                    MyPrinter.Print("Total:                         " + sum.ToString() + "\r\n");
                    MyPrinter.Print("----------------------------------------\r\n");
                    MyPrinter.Print("   Thanks For visit Zia Book Seller.\r\n");
                    MyPrinter.Print("  Change and Return is possible within \r\n");
                    MyPrinter.Print("         2 days with this bill\r\n");
                    MyPrinter.Print("_________________________________________\r\n");
                    MyPrinter.Print("\r\n");
                    MyPrinter.Print("\r\n");
                    MyPrinter.Print("\r\n");
                    MyPrinter.Print("\r\n");
                    MyPrinter.Print("\r\n");
                    MyPrinter.Print("\r\n");
                    MyPrinter.Print("\r\n");
                    MyPrinter.Print("\r\n");

                    MyPrinter.Close();

                    inserting_process();


                    ResetControl(this);
                
               


            }
            else
            {
                MessageBox.Show("Please add product to start process", "Empty Sale", MessageBoxButtons.OK, MessageBoxIcon.Hand);
               
            }


One when I tried to add name of product It does not shows correct name but it shows form name even Name is correctly shown in gridview and even displaying good in message box.
Other problem is that I want to fix with of name in bill. If name is small than other would be spaces and if name is large than extra words would be eliminated.
AnswerRe: Bill Printing Issue Pin
Antonino Porcino3-Mar-13 21:28
Antonino Porcino3-Mar-13 21:28 
QuestionNot working in VB.NET Pin
Peter Yulius10-Jan-13 3:37
Peter Yulius10-Jan-13 3:37 
AnswerRe: Not working in VB.NET Pin
Antonino Porcino10-Jan-13 6:48
Antonino Porcino10-Jan-13 6:48 
GeneralMy vote of 5 Pin
wkiess0115-Jan-12 15:37
wkiess0115-Jan-12 15:37 
QuestionMake gujrati print in C# Pin
CHAVADA SURAJ12-Dec-11 15:03
CHAVADA SURAJ12-Dec-11 15:03 
AnswerRe: Make gujrati print in C# Pin
Antonino Porcino12-Dec-11 21:36
Antonino Porcino12-Dec-11 21:36 
AnswerRe: Make gujrati print in C# Pin
lukac23-Dec-11 0:39
professionallukac23-Dec-11 0:39 
QuestionLine Printer Class problem Pin
Zil-e-Huma16-Aug-11 1:03
Zil-e-Huma16-Aug-11 1:03 
AnswerRe: Line Printer Class problem Pin
Antonino Porcino16-Aug-11 3:55
Antonino Porcino16-Aug-11 3:55 
GeneralRe: Line Printer Class problem Pin
Zil-e-Huma16-Aug-11 17:50
Zil-e-Huma16-Aug-11 17:50 
GeneralRe: Line Printer Class problem Pin
Zil-e-Huma16-Aug-11 19:42
Zil-e-Huma16-Aug-11 19:42 
GeneralRe: Line Printer Class problem Pin
Antonino Porcino16-Aug-11 20:57
Antonino Porcino16-Aug-11 20:57 
GeneralRe: Line Printer Class problem Pin
Zil-e-Huma17-Aug-11 0:06
Zil-e-Huma17-Aug-11 0:06 
GeneralRe: Line Printer Class problem Pin
Antonino Porcino17-Aug-11 1:50
Antonino Porcino17-Aug-11 1:50 
GeneralRe: Line Printer Class problem Pin
Zil-e-Huma17-Aug-11 4:56
Zil-e-Huma17-Aug-11 4:56 
GeneralRe: Line Printer Class problem Pin
Antonino Porcino17-Aug-11 5:32
Antonino Porcino17-Aug-11 5:32 
GeneralRe: Line Printer Class problem Pin
Zil-e-Huma17-Aug-11 18:18
Zil-e-Huma17-Aug-11 18:18 

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.