Click here to Skip to main content
11,584,208 members (37,137 online)
Click here to Skip to main content

An LPR client in C#

, 27 Dec 2006 125.4K 5K 53
Rate this:
Please Sign up or sign in to vote.
An article on printing with LPR in C#.

screenshot of the demo application

Introduction

This article presents a printer class that supports the LPR print protocol in C#. With this class, it is possible to send a print file in ASCII, PostScript, PCL, et cetera, directly to a network printer or print server that communicates by means of the LPR (LPD) protocol. The printer class also implements the LPQ, the LPRM, and the Restart request.

Background

The LPR/LPD protocol is a 15 year old print protocol from the TCP/IP suite that is still important in the area of network printers and print servers. It is described in detail in RFC 1179. Augmented variations of the protocol exist, like LPRNG also known as LPR Next Generation.

In an application, I needed to send PostScript files to an LPD enabled printer. It was possible to use the command line lpr.exe which is included in Windows but I didn't want to be dependant on lpr.exe, so I searched for a free C# implementation of LPR. As I could not find one, it was time to build such a class myself.

How to use the code

The printer class is straightforward. The constructor is called with three parameters, the hostname, the queue name, and the username. As these parameters are reused again and again with every LPR and LPQ request, I decided to place them in the constructor. Just a choice.

To print a file, one only needs to call LPR with the filename as argument, and to get the content of the spool queue, just call LPQ. The boolean parameter of LPQ indicates a long or small listing. The output format of LPQ depends on the implementation of the LPD daemon in the printer so there might be no difference.

So the core code to print a postscript file could look like:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    LPD.Printer printer1 = 
         new LPD.Printer("saturnus","queue","rob");
    string fname = openFileDialog1.FileName;
    if (fname.EndsWith(".ps"))
    {
        printer1.LPR(fname);
        textBox1.Text = printer1.LPQ(false);
    }
    else
    {
        // display appropiate error message
        // ...
    }
}

Points of interest

To use the printer class, one should have a network printer with an LPD daemon, or start the TCP/IP Print Service on a computer (print server). Then, you can send files to any Windows printer defined on the computer. Use the PC name as hostname and the name of the printer as queue name. Be aware that the name of the printers may not contain spaces as the LPR/LPD protocol uses spaces as separators.

LPR does not wait until the file is printed, it starts a thread in the background for every file. LPQ and LPRM do not use a background thread.

The Restart method seems not to be supported in the Windows LPD daemon as I get no acknowledge. Nevertheless, I kept it in the code (use at own risk Smile | :) .

Some things to improve the class include: (no deadline)

  • improve error handling
  • implement status and some other properties
  • fix some todo's in the code
  • overload LPR to print from a stream
  • call back when file prints (e.g. for progress indicator)
  • refactor ad fundum

History

  • 2006/12/24 - Version 1.06 - Added some comments, a status string, InternalQueueSize, and filesSend.
  • 2006/12/24 - Version 1.03 - Added delete flag (thanks to Dion Slijp).
  • 2006/11/09 - Version 1.02 - Patched code with remarks of Karl Fleischmann.
  • 2006/01/14 - Version 1.01 - Added host, queue + user name to demo
  • 2006/01/02 - Version 1.00 - published on CodeProject.
  • 2006/01/02 - Version 0.96 - added Restart, fixed minor bugs, updated CP page.
  • 2005/12/31 - Version 0.92 - added WriteLog, added LPRM.
  • 2005/12/30 - Version 0.90 - refactoring protocode, writing initial CP page.
  • 2005/??/?? - started with the Printer class.

Usage rights

Everybody is granted to use this code as long as you refer to the original work, and I would appreciate that enhancements are published at CodeProject too.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

rob tillaart
Web Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionOption to implement -C parameter ? Pin
Karthikcse19-Jan-15 22:26
memberKarthikcse19-Jan-15 22:26 
QuestionDownload now working Pin
stevenboz17-Oct-13 18:05
memberstevenboz17-Oct-13 18:05 
AnswerRe: Download now working Pin
rob tillaart17-Oct-13 22:00
memberrob tillaart17-Oct-13 22:00 
GeneralThank you! Pin
vicopocho23-Jul-12 4:36
membervicopocho23-Jul-12 4:36 
GeneralMy vote of 5 Pin
xinshg5-Jul-12 15:16
memberxinshg5-Jul-12 15:16 
QuestionIs it possible to specify the tray in PostScript? Pin
zhangzq716-Feb-12 23:03
memberzhangzq716-Feb-12 23:03 
AnswerRe: Is it possible to specify the tray in PostScript? Pin
rob tillaart8-Feb-12 8:31
memberrob tillaart8-Feb-12 8:31 
GeneralRe: Is it possible to specify the tray in PostScript? Pin
zhangzq718-Feb-12 15:51
memberzhangzq718-Feb-12 15:51 
GeneralRe: Is it possible to specify the tray in PostScript? Pin
rob tillaart9-Feb-12 9:51
memberrob tillaart9-Feb-12 9:51 
GeneralRe: Is it possible to specify the tray in PostScript? Pin
zhangzq719-Feb-12 15:36
memberzhangzq719-Feb-12 15:36 
QuestionWindows 7 64-bit Pin
bobrocks7920-Jul-11 9:27
memberbobrocks7920-Jul-11 9:27 
AnswerRe: Windows 7 64-bit [modified] Pin
rob tillaart21-Jul-11 0:14
memberrob tillaart21-Jul-11 0:14 
QuestionNeed Help To Develop IPHONE Printing Pin
Murugan.mcse22-Jun-11 7:03
memberMurugan.mcse22-Jun-11 7:03 
AnswerRe: Need Help To Develop IPHONE Printing Pin
rob tillaart22-Jun-11 22:16
memberrob tillaart22-Jun-11 22:16 
GeneralRe: Need Help To Develop IPHONE Printing Pin
Murugan.mcse23-Jun-11 5:23
memberMurugan.mcse23-Jun-11 5:23 
GeneralRe: Need Help To Develop IPHONE Printing Pin
rob tillaart23-Jun-11 7:11
memberrob tillaart23-Jun-11 7:11 
Generalsupport for lpr binary mode (lpr -o l) added Pin
justin.killen29-Apr-10 5:50
memberjustin.killen29-Apr-10 5:50 
GeneralRe: support for lpr binary mode (lpr -o l) added Pin
rob tillaart3-May-10 7:46
memberrob tillaart3-May-10 7:46 
GeneralUsing PCL to print Pin
vanhungit6-Apr-10 20:52
membervanhungit6-Apr-10 20:52 
GeneralRe: Using PCL to print Pin
rob tillaart6-Apr-10 22:04
memberrob tillaart6-Apr-10 22:04 
GeneralPerformance tweaks Pin
Rowland Shaw14-Dec-09 4:15
memberRowland Shaw14-Dec-09 4:15 
GeneralRe: Performance tweaks Pin
rob tillaart6-Apr-10 22:05
memberrob tillaart6-Apr-10 22:05 
GeneralLess naive implementation of GetJobId() Pin
Rowland Shaw14-Dec-09 4:06
memberRowland Shaw14-Dec-09 4:06 
GeneralLPR failed after 10 jobs [modified] Pin
balu1234510-Oct-09 8:46
memberbalu1234510-Oct-09 8:46 
GeneralRe: LPR failed after 10 jobs Pin
rob tillaart11-Oct-09 7:02
memberrob tillaart11-Oct-09 7:02 
GeneralRe: LPR failed after 10 jobs Pin
balu1234511-Oct-09 7:53
memberbalu1234511-Oct-09 7:53 
GeneralRe: LPR failed after 10 jobs Pin
rob tillaart13-Oct-09 6:48
memberrob tillaart13-Oct-09 6:48 
GeneralRe: LPR failed after 10 jobs Pin
balu1234523-Oct-09 7:36
memberbalu1234523-Oct-09 7:36 
GeneralRe: LPR failed after 10 jobs Pin
rob tillaart23-Oct-09 21:42
memberrob tillaart23-Oct-09 21:42 
GeneralRe: LPR failed after 10 jobs Pin
balu123454-Nov-09 3:02
memberbalu123454-Nov-09 3:02 
GeneralRe: LPR failed after 10 jobs Pin
rob tillaart4-Nov-09 10:43
memberrob tillaart4-Nov-09 10:43 
GeneralRe: LPR failed after 10 jobs Pin
mjohl21-Jan-11 2:33
membermjohl21-Jan-11 2:33 
GeneralRe: LPQ ? Pin
rob tillaart21-Jan-11 4:27
memberrob tillaart21-Jan-11 4:27 
QuestionNot able to print PDF files with demo exe Pin
NEERBADA320-Apr-09 10:16
memberNEERBADA320-Apr-09 10:16 
AnswerRe: Not able to print PDF files with demo exe Pin
rob tillaart20-Apr-09 20:27
memberrob tillaart20-Apr-09 20:27 
GeneralRe: Not able to print PDF files with demo exe Pin
NEERBADA321-Apr-09 0:25
memberNEERBADA321-Apr-09 0:25 
GeneralRe: Not able to print PDF files with demo exe Pin
rob tillaart21-Apr-09 2:32
memberrob tillaart21-Apr-09 2:32 
GeneralRe: Not able to print PDF files with demo exe Pin
NEERBADA321-Apr-09 3:00
memberNEERBADA321-Apr-09 3:00 
GeneralRe: Not able to print PDF files with demo exe Pin
NEERBADA321-Apr-09 7:30
memberNEERBADA321-Apr-09 7:30 
GeneralRe: Not able to print PDF files with demo exe Pin
rob tillaart21-Apr-09 20:18
memberrob tillaart21-Apr-09 20:18 
GeneralRe: Not able to print PDF files with demo exe Pin
NEERBADA322-Apr-09 8:01
memberNEERBADA322-Apr-09 8:01 
GeneralRe: Not able to print PDF files with demo exe Pin
NEERBADA322-Apr-09 9:56
memberNEERBADA322-Apr-09 9:56 
GeneralRe: Not able to print PDF files with demo exe Pin
ddod9-Dec-11 10:43
memberddod9-Dec-11 10:43 
GeneralRe: Not able to print PDF files with demo exe Pin
ddod12-Dec-11 11:33
memberddod12-Dec-11 11:33 
QuestionIs it possible use as a WebService ? Pin
malukinho12-Nov-08 16:41
membermalukinho12-Nov-08 16:41 
AnswerRe: Is it possible use as a WebService ? Pin
rob tillaart12-Nov-08 22:16
memberrob tillaart12-Nov-08 22:16 
QuestionPrinting contents of a textbox to a Netwrok printer using ASP.net Pin
Member 408831815-May-08 4:52
memberMember 408831815-May-08 4:52 
AnswerRe: Printing contents of a textbox to a Netwrok printer using ASP.net Pin
rob tillaart1-Jun-08 3:40
memberrob tillaart1-Jun-08 3:40 
QuestionStatus Pin
jmshearer30-May-07 6:18
memberjmshearer30-May-07 6:18 
AnswerRe: Status Pin
rob tillaart3-Jun-07 1:37
memberrob tillaart3-Jun-07 1:37 

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.150603.1 | Last Updated 27 Dec 2006
Article Copyright 2006 by rob tillaart
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid