Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

HP PCL PCL5 Printing

Rate me:
Please Sign up or sign in to vote.
3.00/5 (14 votes)
22 Apr 2005 111.2K   1.5K   32   19
HP PCL PCL5 printing in C#

Sample image

Introduction

With this code, you can print directly to the printer using WIN32 API calls and therefore should enable you to print at maximum speed. Additionally, there is code to show how to send Hewlett Packard PCL5 codes to the printer.

Using the Code

  • Add a form to your project.
  • Add a button to the form.
  • Change the Name property of the button to btnString.
  • Add another button to the form.
  • Change the Name property of the button to btnSquare.
  • Write the following code for the buttons:
    C#
    private void btnString_Click(object sender, System.EventArgs e) 
    {
       //PCL5 Commands to print a string 
       string st1 = "Printing a string test."; 
       Printing.DirectPrinter.SendToPrinter ("Printing a string",st1,
                 "\\\\192.168.1.101\\hpl"); 
    } 
    
    private void btnSquare_Click(object sender, System.EventArgs e) 
    { 
       // PCL5 Commands to print a square 
       // Moves the cursor 900 dots (3 inches at 300 dpi) in from the 
       // left margin, and 600 dots (2 inches at 300 dpi) down from 
       // the top margin. 
       string st1="*p900x600Y"; 
       // Using the print model commands for rectangle dimensions, 
       // "600a" specifies a rectangle with a horizontal size 
       // or width of 600 dots, and "6b" specifies 
       // a vertical size or height of 6 dots. The 0P selects the solid black 
       // rectangular area fill. 
       st1+="*c600a6b0P"; 
       // Specifies a rectangle with width of 6 dots, height of 600 dots, and a 
       // fill pattern of solid black. 
       st1+="*c6a600b0P"; 
       // Moves the current cursor position to 900 dots, from the left margin and 
       // 1200 dots down from the top margin. 
       st1+="*p900x1200Y"; 
       // Specifies a rectangle with a width of 606 dots, a height of 6 dots and a 
       // fill pattern of solid black. 
       st1+="*c606a6b0P"; 
       // Moves the current cursor position to 1500 dots from the left margin and 
       // 600 dots down from the top margin. 
       st1+="*p1500x600Y"; 
       // Specifies a rectangle with a width of 6 dots, a height of 600 dots and a 
       // fill pattern of solid black. 
       st1+="*c6a600b0P"; 
       // Send a form feed character to the printer 
       st1+="\f"; 
       Printing.DirectPrinter.SendToPrinter ("Printing a square",st1,
                  "\\\\192.168.1.101\\hpl"); 
    }

    Now:

  • Add a new class to the project called DirectPrinter.cs.
    C#
    // This code is based on a sample Written 17th October 2002 By <a href="mailto:csharpconsulting@hotmail.com">J O'Donnell</a>
    // Adapted from Microsoft Support article Q298141 using System; 
    //For PCL5
    using System.Text;
    using System.Runtime.InteropServices; 
    [StructLayout( LayoutKind.Sequential)] 
    public struct DOCINFO 
    { 
      [MarshalAs(UnmanagedType.LPWStr)] public stringpDocName;
      [MarshalAs(UnmanagedType.LPWStr)] public stringpOutputFile;
      [MarshalAs(UnmanagedType.LPWStr)] public string pDataType; 
    }
    namespace Printing 
    { 
      public class DirectPrinter 
      { 
        public DirectPrinter() 
        {
          //
          // TODO: Add constructor logic here 
          // 
        } 
        [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false, 
                       CallingConvention=CallingConvention.StdCall )] 
        public static extern long OpenPrinter(string pPrinterName,
                       ref IntPtr phPrinter, int pDefault);
        [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false, 
                       CallingConvention=CallingConvention.StdCall )] 
        public static extern long StartDocPrinter(IntPtr hPrinter, 
                       int Level, ref DOCINFO pDocInfo); 
        [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true, 
                       CallingConvention=CallingConvention.StdCall)] 
        public static extern long StartPagePrinter(IntPtr hPrinter); 
        [ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true, 
                       CallingConvention=CallingConvention.StdCall)] 
        public static extern long WritePrinter(IntPtr hPrinter,string data, 
                       int buf,ref int pcWritten); 
        [ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true, 
                       CallingConvention=CallingConvention.StdCall)] 
        public static extern long EndPagePrinter(IntPtr hPrinter); 
        [ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true, 
                       CallingConvention=CallingConvention.StdCall)] 
        public static extern long EndDocPrinter(IntPtr hPrinter); 
        [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true, 
                       CallingConvention=CallingConvention.StdCall )] 
        public static extern long ClosePrinter(IntPtr hPrinter); 
    
        public static void SendToPrinter(string jobName, 
                       string PCL5Commands, string printerName) 
        { 
          System.IntPtr lhPrinter=new System.IntPtr();
          DOCINFO di = new DOCINFO(); 
          int pcWritten=0; 
          di.pDocName=jobName; 
          di.pDataType="RAW"; 
          //lhPrinter contains the handle for the printer opened 
          //If lhPrinter is 0 then an error has occurred 
          // This code assumes you have a printer at share \\192.168.1.101\hpl 
          // This code sends Hewlett Packard PCL5 codes to the printer 
          //OpenPrinter("\\\\192.168.1.101\\hpl",ref lhPrinter,0); 
          OpenPrinter(printerName,ref lhPrinter,0); 
          StartDocPrinter(lhPrinter,1,ref di); 
          StartPagePrinter(lhPrinter); 
          WritePrinter(lhPrinter,PCL5Commands,PCL5Commands.Length,ref pcWritten); 
          EndPagePrinter(lhPrinter); 
          EndDocPrinter(lhPrinter); 
          ClosePrinter(lhPrinter); 
        } 
      } 
    }

If you have any questions, please leave a comment below.

History

  • Version 1.0 - Initial release

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.


Written By
Web Developer
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to select upper tray ? Pin
nguyenchauhuyen29-Nov-10 23:18
nguyenchauhuyen29-Nov-10 23:18 
Questionwhy? Pin
vicentewang198323-Feb-09 21:29
vicentewang198323-Feb-09 21:29 
GeneralAdd signature to document Pin
jsagno8-Jan-07 7:47
jsagno8-Jan-07 7:47 
GeneralPrinting PCL images in C# [modified] Pin
thrawn4225-May-06 11:18
thrawn4225-May-06 11:18 
QuestionPrint a file Pin
samsagaz4-May-06 5:12
samsagaz4-May-06 5:12 
AnswerRe: Print a file Pin
thapa da man13-Jul-06 1:56
thapa da man13-Jul-06 1:56 
Generalfull length Pin
ken_ng31-Jan-06 2:51
ken_ng31-Jan-06 2:51 
hi i was wondering if anyone is having problem with this code. It wont print until the entire length is used like if the page is 42 char long it wont print until the entire 42 is used. I am using a pos printer for those that are interested but i dont thing that would cause a problem..... anyone know y?
GeneralRe: full length Pin
fhunth31-Jan-06 8:16
fhunth31-Jan-06 8:16 
GeneralTHANK YOU Pin
marcelmedina26-Oct-05 9:12
marcelmedina26-Oct-05 9:12 
GeneralRe: THANK YOU Pin
Anonymous26-Oct-05 10:28
Anonymous26-Oct-05 10:28 
GeneralWorth a 5 to Me! Pin
ajstadlin13-Jun-05 8:13
ajstadlin13-Jun-05 8:13 
GeneralJust what I was looking for Pin
Simon Hughes1-Apr-05 3:43
Simon Hughes1-Apr-05 3:43 
GeneralRe: Just what I was looking for Pin
NormDroid27-Jul-05 9:18
professionalNormDroid27-Jul-05 9:18 
GeneralPCL5 and 6 Pin
Michael Hawksworth2-Feb-05 22:55
Michael Hawksworth2-Feb-05 22:55 
GeneralRe: PCL5 and 6 Pin
fhunth3-Feb-05 2:28
fhunth3-Feb-05 2:28 
QuestionWhy? Pin
Ashaman20-Jan-05 6:53
Ashaman20-Jan-05 6:53 
AnswerRe: Why? Pin
fhunth20-Jan-05 7:14
fhunth20-Jan-05 7:14 
GeneralRe: Why? Pin
WillemM20-Jan-05 7:26
WillemM20-Jan-05 7:26 
GeneralRe: Why? Pin
Ashaman20-Jan-05 8:04
Ashaman20-Jan-05 8:04 

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.