Click here to Skip to main content
15,891,253 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.4K   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

 
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 
GeneralRe: Line Printer Class problem Pin
Antonino Porcino17-Aug-11 22:26
Antonino Porcino17-Aug-11 22:26 
GeneralRe: Line Printer Class problem Pin
Zil-e-Huma17-Aug-11 23:04
Zil-e-Huma17-Aug-11 23:04 
GeneralRe: Line Printer Class problem Pin
Zil-e-Huma17-Aug-11 23:32
Zil-e-Huma17-Aug-11 23:32 
GeneralRe: Line Printer Class problem Pin
Antonino Porcino18-Aug-11 0:09
Antonino Porcino18-Aug-11 0:09 
GeneralRe: Line Printer Class problem Pin
Zil-e-Huma17-Aug-11 22:13
Zil-e-Huma17-Aug-11 22:13 
Yes your test code is printing out a dotted rectangle.
but my bitmap printing is not giving any esults, it is printing out just nothing.. It shows that i am not handling the bitnmap image correctly in the PrintBitmap function. i have displayed that image into a picture box, and the picture box is showing correcrt results, it means that that bitmao has been created succesfully but there is some problem with my handling that image into the PrintBitmap() function. Kindly help me in this regard.

The printer which i am using is thermal printer, Brother pocketJet3 Plus (PJ523).

I have debugged the code to check the values of buf, b, str etc.
b=27;
buf[0]=27;
str value is something i cant understand. it is just a box displayed there, perhaps that is a garbage value or something i dont know.

Kindly guide me in this regard. i shall be very thankful.

Here is my code, i am pasting only those functions which are creating problem.


stem.Drawing.Bitmap CreateBitmap()
{
int x, y;
System.Drawing.Bitmap flag = new System.Drawing.Bitmap(300, 300);
for (x = 0; x < flag.Height; ++x)
for (y = 0; y < flag.Width; ++y)
flag.SetPixel(x, y, Color.White);
for (x = 0; x < flag.Height; ++x)
flag.SetPixel(x, x, Color.Black);
return flag;
}


//the print button
private void button2_Click(object sender, EventArgs e)
{
if (!MyPrinter.Open("Test Page")) return;
PrintBitmap();
//MyPrinter.Print("\n\r");
MyPrinter.Close();
}


public void PrintByte(int b)
{
byte[] buf = new byte[1];
buf[0] = (byte)b;
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
string str = enc.GetString(buf);
MyPrinter.Print(str);
}


public void PrintBitmap()
{
System.Drawing.Bitmap flag;
flag = CreateBitmap(); //calling the function which creates bitmap
pictureBox1.Image = flag;
int width = flag.Width;
int height = flag.Height - flag.Height % 8;
int numbytes = height;
int lo = numbytes & 255;
int hi = numbytes >> 8;
for (int y = 0; y < height; y += 8)
{
PrintByte(27);
PrintByte(75);
PrintByte(lo);
PrintByte(hi);
for (int x = 0; x < width; x++)
{
int b = 0;
for (int i = 0; i <= 7; i++)
{
Color c = flag.GetPixel(x, y + i);
if (c == Color.Black)
b = b | (1 << (7 - i));
}
PrintByte(b);
}
}

}

}
Questionprint image ? Pin
Herman_6-Aug-11 5:06
Herman_6-Aug-11 5:06 
GeneralPrint UniCode characters and other font size Pin
unodei24-Sep-10 19:58
unodei24-Sep-10 19:58 
GeneralRe: Print UniCode characters and other font size Pin
Antonino Porcino24-Sep-10 23:45
Antonino Porcino24-Sep-10 23:45 
GeneralRe: Print UniCode characters and other font size Pin
unodei25-Sep-10 2:32
unodei25-Sep-10 2:32 
GeneralRe: Print UniCode characters and other font size Pin
Antonino Porcino25-Sep-10 7:18
Antonino Porcino25-Sep-10 7:18 
GeneralRe: Print UniCode characters and other font size Pin
unodei26-Sep-10 14:31
unodei26-Sep-10 14:31 
GeneralRe: Print UniCode characters and other font size Pin
unodei26-Sep-10 14:34
unodei26-Sep-10 14:34 
GeneralRe: Print UniCode characters and other font size Pin
Antonino Porcino26-Sep-10 21:01
Antonino Porcino26-Sep-10 21:01 
GeneralRe: Print UniCode characters and other font size Pin
unodei26-Sep-10 21:03
unodei26-Sep-10 21:03 
GeneralRe: Print UniCode characters and other font size Pin
unodei3-Oct-10 20:46
unodei3-Oct-10 20:46 
GeneralRe: Print UniCode characters and other font size Pin
Antonino Porcino3-Oct-10 23:39
Antonino Porcino3-Oct-10 23:39 
QuestionHow to set Font and Font Size Pin
unodei20-Sep-10 21:36
unodei20-Sep-10 21:36 
AnswerRe: How to set Font and Font Size Pin
Antonino Porcino20-Sep-10 22:31
Antonino Porcino20-Sep-10 22:31 
GeneralRe: How to set Font and Font Size Pin
unodei20-Sep-10 22:46
unodei20-Sep-10 22:46 
GeneralRe: How to set Font and Font Size Pin
unodei20-Sep-10 22:48
unodei20-Sep-10 22:48 
GeneralRe: How to set Font and Font Size Pin
unodei20-Sep-10 22:52
unodei20-Sep-10 22:52 
GeneralRe: How to set Font and Font Size Pin
Antonino Porcino21-Sep-10 0:05
Antonino Porcino21-Sep-10 0:05 

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.