Click here to Skip to main content
Click here to Skip to main content

HP PCL PCL5 Printing

, 22 Apr 2005
Rate this:
Please Sign up or sign in to vote.
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.
    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.
    // 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 occured 
          // This code assumes you have a printer at share \\<A href="file://192.168.1.101/hpl" target=_blank>192.168.1.101\hpl</A> 
          // 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); 
        } 
      } 
    }

History

Version 1.0 - Initial release.

Contact me at fhunth@hotmail.com or fernando@huddle.com.ar.

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

fhunth
Web Developer
Argentina Argentina
No Biography provided

Comments and Discussions

 
Questionhow to select upper tray ? Pinmembernguyenchauhuyen29-Nov-10 23:18 
Questionwhy? Pinmembervicentewang198323-Feb-09 21:29 
GeneralAdd signature to document Pinmemberjsagno8-Jan-07 7:47 
GeneralPrinting PCL images in C# [modified] Pinmemberthrawn4225-May-06 11:18 
QuestionPrint a file Pinmembersamsagaz4-May-06 5:12 
AnswerRe: Print a file Pinmemberthapa da man13-Jul-06 1:56 
Generalfull length Pinmemberken_ng31-Jan-06 2:51 
GeneralRe: full length Pinmemberfhunth31-Jan-06 8:16 
GeneralTHANK YOU Pinmembermarcelmedina26-Oct-05 9:12 
GeneralRe: THANK YOU PinsussAnonymous26-Oct-05 10:28 
GeneralWorth a 5 to Me! Pinmemberajstadlin13-Jun-05 8:13 
GeneralJust what I was looking for PinmemberSimon Hughes1-Apr-05 3:43 
GeneralRe: Just what I was looking for Pinmembernorm.net27-Jul-05 9:18 
GeneralPCL5 and 6 PinmemberMichael Hawksworth2-Feb-05 22:55 
GeneralRe: PCL5 and 6 PinsussFernando Hunth3-Feb-05 2:28 
QuestionWhy? PinmemberAshaman20-Jan-05 6:53 
AnswerRe: Why? PinsussFernando Hunth20-Jan-05 7:14 
GeneralRe: Why? PinmemberWillemM20-Jan-05 7:26 
GeneralRe: Why? PinmemberAshaman20-Jan-05 8:04 

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 | Mobile
Web03 | 2.8.140827.1 | Last Updated 22 Apr 2005
Article Copyright 2005 by fhunth
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid