打印连续纸张的时候,经常会遇到需要自动进退纸的功能,譬如下列场景:
1. 打印完一个小标;(自动进纸)
2. 用户撕断打印出来的小标;
3. (自动退纸,纸张归位) 接着打印下一个小标;
下列代码在爱普生LQ 730k打印机上测试通过:
1: //使用示例
2: new PrintDirect("PrinterName").PrintESC(1);//打印后进纸
3: new PrintDirect("PrinterName").PrintESC(0);//打印前退纸
源代码:
1:
2: [StructLayout(LayoutKind.Sequential)]
3: public struct DOCINFO
4: {
5: [MarshalAs(UnmanagedType.LPWStr)]
6: public string pDocName;
7: [MarshalAs(UnmanagedType.LPWStr)]
8: public string pOutputFile;
9: [MarshalAs(UnmanagedType.LPWStr)]
10: public string pDataType;
11: }
12:
13: public class PrintDirect
14: {
15: private string PrintPort { get; set; }
16: public PrintDirect(string port)
17: {
18: this.PrintPort = port;
19: }
20:
21: [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
22: public static extern long OpenPrinter(string pPrinterName, ref IntPtr phPrinter, int pDefault);
23:
24: [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
25: public static extern long StartDocPrinter(IntPtr hPrinter, int Level, ref DOCINFO pDocInfo);
26:
27: [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
28: public static extern long StartPagePrinter(IntPtr hPrinter);
29:
30: [DllImport("winspool.drv", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
31: public static extern long WritePrinter(IntPtr hPrinter, string data, int buf, ref int pcWritten);
32:
33: [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
34: public static extern long EndPagePrinter(IntPtr hPrinter);
35:
36: [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
37: public static extern long EndDocPrinter(IntPtr hPrinter);
38:
39: [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
40: public static extern long ClosePrinter(IntPtr hPrinter);
41:
42: public void PrintESC(int type)
43: {
44: System.IntPtr lhPrinter = new System.IntPtr();
45: DOCINFO di = new DOCINFO();
46: int pcWritten = 0;
47: di.pDocName = "进退纸Document";
48: di.pDataType = "RAW";
49:
50: try
51: {
52: PrintDirect.OpenPrinter(this.PrintPort, ref lhPrinter, 0);
53: if (lhPrinter == IntPtr.Zero)
54: {
55: MessageBox.Show(string.Format("没有连接打印机或者打印机端口不是{0}!", this.PrintPort),
56: "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
57: return;
58: }
59: PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
60: PrintDirect.StartPagePrinter(lhPrinter);
61: string send = string.Empty;
62: for (int j = 0; j < 3; j++)
63: {
64: switch (type)
65: {
66: case 0:
67: send = "" + (char)(27) + (char)(64) + (char)(27) + 'j' + (char)(180);//退纸
68: break;
69: case 1:
70: send = "" + (char)(27) + (char)(64) + (char)(27) + 'J' + (char)(180);//进纸
71: break;
72: case 2:
73: send = "" + (char)(27) + (char)(64) + (char)(12); //换行(未测试)
74: break;
75: default:
76: send = "" + (char)(27) + (char)(64) + (char)(12); //换行(未测试)
77: break;
78: }
79: byte[] buf = new byte[80];
80: for (int i = 0; i < send.Length; i++)
81: {
82: buf[i] = (byte)send[i];
83: }
84: PrintDirect.WritePrinter(lhPrinter, send, send.Length, ref pcWritten);
85: }
86: }
87: catch (Exception e)
88: {
89: MessageBox.Show(e.Message);
90: }
91: finally
92: {
93: if (lhPrinter != IntPtr.Zero)
94: {
95: PrintDirect.EndPagePrinter(lhPrinter);
96: PrintDirect.EndDocPrinter(lhPrinter);
97: PrintDirect.ClosePrinter(lhPrinter);
98: }
99: }
100: }
101: }