|
|
@@ -0,0 +1,124 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Runtime.InteropServices;
|
|
|
+using System.Text;
|
|
|
+
|
|
|
+namespace UAS_Web.tool
|
|
|
+{
|
|
|
+ class PrintHelper
|
|
|
+ {
|
|
|
+
|
|
|
+ // 结构和API declarions
|
|
|
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
|
|
+ public class DOCINFOA
|
|
|
+ {
|
|
|
+ [MarshalAs(UnmanagedType.LPStr)]
|
|
|
+ public string pDocName;
|
|
|
+ [MarshalAs(UnmanagedType.LPStr)]
|
|
|
+ public string pOutputFile;
|
|
|
+ [MarshalAs(UnmanagedType.LPStr)]
|
|
|
+ public string pDataType;
|
|
|
+ }
|
|
|
+ [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
|
|
+ public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
|
|
|
+
|
|
|
+ [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
|
|
+ public static extern bool ClosePrinter(IntPtr hPrinter);
|
|
|
+
|
|
|
+ [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
|
|
+ public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
|
|
|
+
|
|
|
+ [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
|
|
+ public static extern bool EndDocPrinter(IntPtr hPrinter);
|
|
|
+
|
|
|
+ [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
|
|
+ public static extern bool StartPagePrinter(IntPtr hPrinter);
|
|
|
+
|
|
|
+ [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
|
|
+ public static extern bool EndPagePrinter(IntPtr hPrinter);
|
|
|
+
|
|
|
+ [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
|
|
+ public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
|
|
|
+
|
|
|
+ // SendBytesToPrinter()
|
|
|
+ //当函数是给定一个打印机名称和一个非托管数组
|
|
|
+ // 如果执行成功,将返回true;如果执行失败将返回false
|
|
|
+ public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
|
|
|
+ {
|
|
|
+ Int32 dwError = 0, dwWritten = 0;
|
|
|
+ IntPtr hPrinter = new IntPtr(0);
|
|
|
+ DOCINFOA di = new DOCINFOA();
|
|
|
+ bool bSuccess = false; // 假设失败.
|
|
|
+
|
|
|
+ di.pDocName = "My C#.NET RAW Document";
|
|
|
+ di.pDataType = "RAW";
|
|
|
+
|
|
|
+ // 打开打印机
|
|
|
+ if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
|
|
|
+ {
|
|
|
+ // 开始一个文档.
|
|
|
+ if (StartDocPrinter(hPrinter, 1, di))
|
|
|
+ {
|
|
|
+ // 创建新页面
|
|
|
+ if (StartPagePrinter(hPrinter))
|
|
|
+ {
|
|
|
+ // 写你的字节
|
|
|
+ bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
|
|
|
+ EndPagePrinter(hPrinter);
|
|
|
+ }
|
|
|
+ EndDocPrinter(hPrinter);
|
|
|
+ }
|
|
|
+ ClosePrinter(hPrinter);
|
|
|
+ }
|
|
|
+ // 如果没有成功,可能会给更多的信息GetLastError
|
|
|
+ if (bSuccess == false)
|
|
|
+ {
|
|
|
+ dwError = Marshal.GetLastWin32Error();
|
|
|
+ }
|
|
|
+ return bSuccess;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static bool SendFileToPrinter(string szPrinterName, string szFileName)
|
|
|
+ {
|
|
|
+ // 打开文件.
|
|
|
+ FileStream fs = new FileStream(szFileName, FileMode.Open);
|
|
|
+ // 创建一个主题的文件.
|
|
|
+ BinaryReader br = new BinaryReader(fs);
|
|
|
+ // .
|
|
|
+ Byte[] bytes = new Byte[fs.Length];
|
|
|
+ bool bSuccess = false;
|
|
|
+ // 非托管指针
|
|
|
+ IntPtr pUnmanagedBytes = new IntPtr(0);
|
|
|
+ int nLength;
|
|
|
+
|
|
|
+ nLength = Convert.ToInt32(fs.Length);
|
|
|
+ // 读取文件的内容放入数组中.
|
|
|
+ bytes = br.ReadBytes(nLength);
|
|
|
+ // 分配一些非托管内存对于那些字节
|
|
|
+ pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
|
|
|
+ // 复制管理到非托管数组的字节数组
|
|
|
+ Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
|
|
|
+ // 发送到打印机的非托管的字节
|
|
|
+ bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
|
|
|
+ // 非托管内存分配
|
|
|
+ Marshal.FreeCoTaskMem(pUnmanagedBytes);
|
|
|
+ return bSuccess;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static bool SendStringToPrinter(string szPrinterName, string szString)
|
|
|
+ {
|
|
|
+ IntPtr pBytes;
|
|
|
+ Int32 dwCount;
|
|
|
+ // 有多少字符在字符串
|
|
|
+ dwCount = szString.Length;
|
|
|
+ // 假设打印机期待ANSI的文本,然后将字符串ANSI文本。
|
|
|
+ pBytes = Marshal.StringToCoTaskMemAnsi(szString);
|
|
|
+ // 转换后的字符串发送到打印机的ANSI
|
|
|
+ SendBytesToPrinter(szPrinterName, pBytes, dwCount);
|
|
|
+ Marshal.FreeCoTaskMem(pBytes);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|