Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

HP PCL PCL5 Printing

0.00/5 (No votes)
22 Apr 2005 2  
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 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.

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