Using this code enables you to print directly to the printer using WIN32 api calls and therefore should enable you to print at maximum speed rather than relying in the Windows Printing subsystems. Additionally I have added code to show how to send PCL codes to the printer.
The code has been compiled using the Everett beta of Visual Studio.NET however if you have not got this just copy and paste the code into a console project and recompile.
-
-
-
-
-
-
-
- using System;
- using System.Text;
- using System.Runtime.InteropServices;
- [StructLayout(LayoutKind.Sequential)]
- public struct DOCINFO {
- [MarshalAs(UnmanagedType.LPWStr)] public string pDocName;
- [MarshalAs(UnmanagedType.LPWStr)] public string pOutputFile;
- [MarshalAs(UnmanagedType.LPWStr)] public string pDataType;
- }
- public class PrintDirect {
- [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 class App {
- public static void Main() {
- System.IntPtr lhPrinter = new System.IntPtr();
- DOCINFO di = new DOCINFO();
- int pcWritten = 0;
- string st1;
-
- st1 = "This is an example of printing directly to a printer\f";
- di.pDocName = "my test document";
- di.pDataType = "RAW";
-
- st1 = "\x1b*c600a6b0P\f";
-
-
- PrintDirect.OpenPrinter("\\\\192.168.1.101\\hpl", ref lhPrinter, 0);
- PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
- PrintDirect.StartPagePrinter(lhPrinter);
- try {
-
-
- st1 = "\x1b*p900x600Y";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
-
- rectangle
-
-
- st1 = "\x1b*c600a6b0P";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
-
-
- st1 = "\x1b*c6a600b0P";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
-
-
- st1 = "\x1b*p900x1200Y";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
-
-
- st1 = "\x1b*c606a6b0P";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
-
-
- st1 = "\x1b*p1500x600Y";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
-
-
- st1 = "\x1b*c6a600b0P";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
- st1 = "\f";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
- } catch (Exception e) {
- Console.WriteLine(e.Message);
- }
- PrintDirect.EndPagePrinter(lhPrinter);
- PrintDirect.EndDocPrinter(lhPrinter);
- PrintDirect.ClosePrinter(lhPrinter);
- }
- }