Click here to Skip to main content
6,928,573 members and growing! (17,595 online)
Email Password   helpLost your password?
Desktop Development » Printing » General     Intermediate

An LPR client in C#

By rob tillaart

An article on printing with LPR in C#.
C#, Windows, .NET, Visual-Studio, Dev
Posted:11 Jan 2006
Updated:27 Dec 2006
Views:57,364
Bookmarked:40 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 4.21 Rating: 3.78 out of 5
1 vote, 7.7%
1
1 vote, 7.7%
2

3
4 votes, 30.8%
4
7 votes, 53.8%
5

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 :).

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

About the Author

rob tillaart


Member

Occupation: Web Developer
Location: Netherlands Netherlands

Other popular Printing articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 39 (Total in Forum: 39) (Refresh)FirstPrevNext
GeneralPerformance tweaks PinmemberRowland Shaw5:15 14 Dec '09  
GeneralLess naive implementation of GetJobId() PinmemberRowland Shaw5:06 14 Dec '09  
GeneralLPR failed after 10 jobs [modified] Pinmemberbalu123459:46 10 Oct '09  
GeneralRe: LPR failed after 10 jobs Pinmemberrob tillaart8:02 11 Oct '09  
GeneralRe: LPR failed after 10 jobs Pinmemberbalu123458:53 11 Oct '09  
GeneralRe: LPR failed after 10 jobs Pinmemberrob tillaart7:48 13 Oct '09  
GeneralRe: LPR failed after 10 jobs Pinmemberbalu123458:36 23 Oct '09  
GeneralRe: LPR failed after 10 jobs Pinmemberrob tillaart22:42 23 Oct '09  
GeneralRe: LPR failed after 10 jobs Pinmemberbalu123454:02 4 Nov '09  
GeneralRe: LPR failed after 10 jobs Pinmemberrob tillaart11:43 4 Nov '09  
QuestionNot able to print PDF files with demo exe PinmemberNEERBADA311:16 20 Apr '09  
AnswerRe: Not able to print PDF files with demo exe Pinmemberrob tillaart21:27 20 Apr '09  
Hi Neerbada,

LPR is a transport protocol for sending printjobs from a client to a printer or printserver. It takes care that a file is sent to a printer with the (elementary) metadata needed to handle a printjob as one entity. Most important it separates two printjobs so they do not overlap on paper, a problem that occured when jobs were sent to fast after each other to old lineprinters.

LPR and LPD do not interpret PostScript or PDF data, so if the printer does not support PDF, LPR will sent it but the interpreter will assume it is plain text resulting in lots of text and numbers. If you need an interpreter for PDF you might google for Ghostscript or contact Adobe for their (commercial) PDF interpreter.

I hope this explains the results you see.

Regards,
Rob
GeneralRe: Not able to print PDF files with demo exe PinmemberNEERBADA31:25 21 Apr '09  
GeneralRe: Not able to print PDF files with demo exe Pinmemberrob tillaart3:32 21 Apr '09  
GeneralRe: Not able to print PDF files with demo exe PinmemberNEERBADA34:00 21 Apr '09  
GeneralRe: Not able to print PDF files with demo exe PinmemberNEERBADA38:30 21 Apr '09  
GeneralRe: Not able to print PDF files with demo exe Pinmemberrob tillaart21:18 21 Apr '09  
GeneralRe: Not able to print PDF files with demo exe PinmemberNEERBADA39:01 22 Apr '09  
GeneralRe: Not able to print PDF files with demo exe PinmemberNEERBADA310:56 22 Apr '09  
GeneralIs it possible use as a WebService ? Pinmembermalukinho17:41 12 Nov '08  
GeneralRe: Is it possible use as a WebService ? Pinmemberrob tillaart23:16 12 Nov '08  
QuestionPrinting contents of a textbox to a Netwrok printer using ASP.net PinmemberMember 40883185:52 15 May '08  
AnswerRe: Printing contents of a textbox to a Netwrok printer using ASP.net Pinmemberrob tillaart4:40 1 Jun '08  
QuestionStatus Pinmemberjmshearer7:18 30 May '07  
AnswerRe: Status Pinmemberrob tillaart2:37 3 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 27 Dec 2006
Editor: Smitha Vijayan
Copyright 2006 by rob tillaart
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project