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.
- // PrintDirect.cs
- // Shows how to write data directly to the printer using Win32 API's
- // Written 17th October 2002 By J O'Donnell - [email protected]
- // Adapted from Microsoft Support article Q298141
- // This code assumes you have a printer at share \\192.168.1.101\hpl
- // This code sends Hewlett Packard PCL5 codes to the printer to print
- // out a rectangle in the middle of the page.
- 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;
- // text to print with a form feed character
- st1 = "This is an example of printing directly to a printer\f";
- di.pDocName = "my test document";
- di.pDataType = "RAW";
- // the \x1b means an ascii escape character
- st1 = "\x1b*c600a6b0P\f";
- //lhPrinter contains the handle for the printer opened
- //If lhPrinter is 0 then an error has occured
- PrintDirect.OpenPrinter("\\\\192.168.1.101\\hpl", ref lhPrinter, 0);
- PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
- PrintDirect.StartPagePrinter(lhPrinter);
- try {
- // 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.
- st1 = "\x1b*p900x600Y";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
- // 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 = "\x1b*c600a6b0P";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
- // Specifies a rectangle with width of 6 dots, height of 600 dots, and a
- // fill pattern of solid black.
- st1 = "\x1b*c6a600b0P";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
- // Moves the current cursor position to 900 dots, from the left margin and
- // 1200 dots down from the top margin.
- st1 = "\x1b*p900x1200Y";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
- // Specifies a rectangle with a width of 606 dots, a height of 6 dots and a
- // fill pattern of solid black.
- st1 = "\x1b*c606a6b0P";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
- // Moves the current cursor position to 1500 dots from the left margin and
- // 600 dots down from the top margin.
- st1 = "\x1b*p1500x600Y";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten);
- // Specifies a rectangle with a width of 6 dots, a height of 600 dots and a
- // fill pattern of solid black.
- st1 = "\x1b*c6a600b0P";
- PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, ref pcWritten); // Send a form feed character to the printer
- 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);
- }
- }
Dionisio Jose Carmona LoraPosted Apr 28, 2022, 8:17 PM
Thanks for this post
Dale SmithPosted Mar 9, 2018, 11:39 AM
Thanks for this post, this helped me setup a form feed operation.
Dhivya pPosted Oct 12, 2017, 5:28 AM
I need to print .prn file, I it possible with the above code?
Dhivya pPosted Oct 12, 2017, 5:28 AM
I need to .prn file
Shameel MuhammadPosted Sep 11, 2017, 1:35 PM
Does this code work for non-text contents?
Sujeet SinghPosted Mar 1, 2017, 6:03 AM
It is not working, Status of the printer is spooling and does not print.
Aslam ShaikhPosted Feb 10, 2017, 4:22 AM
Its not working for me please could you help me..If i am change di.pDataType="RAW" is "Text" then i will got the print with byts code not text and within di.pDataType="RAW" every thing goes executed but not getting print. please help.
LouisePosted Oct 11, 2012, 3:39 PM
I didn't know a <a href="http://www.nwd-microage.com/Index_EN.html">printer</a> could even work that way! This is great--I can go in and change code so that I can not have the wait for my printer to communicate with any one program, I can just have it straight to the printer. Thank you!
kiruba karanPosted Apr 7, 2011, 6:00 AM
Hi, Can u please advice me , How can i send coordinates for the particular text. regards, kiruba
Akash SenPosted Aug 11, 2009, 8:46 AM
We have web application in aspx.net 2.0 with Crystal 11.0. We have rpt which we need print from the JavaScript on client's printer. Could you please help? Regards Akash
Srivatsa HaridasPosted Jul 2, 2009, 5:03 AM
How to print pdf documents in this case ?
Priyanjith FonsekaPosted Mar 13, 2009, 2:02 AM
I was looking this kind of article as i'm designing a direct printing all the big report. once the data gathering process completed, i need them to print directly from the system. I hope this coding will guid me perfectly. thank u John
sapseditedPosted Feb 7, 2006, 3:29 AMEdited Apr 13, 2006, 3:56 PM
Hi John, In my application the user may want to print the current page from a long html file stored on the local PC. My Requirement is that the page gets auomatically printed without prompting to the user and also only 1 single page gets printed that is being currently viewed. Is that possible? I have also printed some other document in the application where PrintDocument class is being used for the purpose of printing. Can that be done to print the html page.. Thanks..