PrintHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace UAS_Web.tool
  8. {
  9. class PrintHelper
  10. {
  11. // 结构和API declarions
  12. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  13. public class DOCINFOA
  14. {
  15. [MarshalAs(UnmanagedType.LPStr)]
  16. public string pDocName;
  17. [MarshalAs(UnmanagedType.LPStr)]
  18. public string pOutputFile;
  19. [MarshalAs(UnmanagedType.LPStr)]
  20. public string pDataType;
  21. }
  22. [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  23. public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
  24. [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  25. public static extern bool ClosePrinter(IntPtr hPrinter);
  26. [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  27. public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
  28. [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  29. public static extern bool EndDocPrinter(IntPtr hPrinter);
  30. [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  31. public static extern bool StartPagePrinter(IntPtr hPrinter);
  32. [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  33. public static extern bool EndPagePrinter(IntPtr hPrinter);
  34. [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  35. public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
  36. // SendBytesToPrinter()
  37. //当函数是给定一个打印机名称和一个非托管数组
  38. // 如果执行成功,将返回true;如果执行失败将返回false
  39. public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
  40. {
  41. Int32 dwError = 0, dwWritten = 0;
  42. IntPtr hPrinter = new IntPtr(0);
  43. DOCINFOA di = new DOCINFOA();
  44. bool bSuccess = false; // 假设失败.
  45. di.pDocName = "My C#.NET RAW Document";
  46. di.pDataType = "RAW";
  47. // 打开打印机
  48. if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
  49. {
  50. // 开始一个文档.
  51. if (StartDocPrinter(hPrinter, 1, di))
  52. {
  53. // 创建新页面
  54. if (StartPagePrinter(hPrinter))
  55. {
  56. // 写你的字节
  57. bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
  58. EndPagePrinter(hPrinter);
  59. }
  60. EndDocPrinter(hPrinter);
  61. }
  62. ClosePrinter(hPrinter);
  63. }
  64. // 如果没有成功,可能会给更多的信息GetLastError
  65. if (bSuccess == false)
  66. {
  67. dwError = Marshal.GetLastWin32Error();
  68. }
  69. return bSuccess;
  70. }
  71. public static bool SendFileToPrinter(string szPrinterName, string szFileName)
  72. {
  73. // 打开文件.
  74. FileStream fs = new FileStream(szFileName, FileMode.Open);
  75. // 创建一个主题的文件.
  76. BinaryReader br = new BinaryReader(fs);
  77. // .
  78. Byte[] bytes = new Byte[fs.Length];
  79. bool bSuccess = false;
  80. // 非托管指针
  81. IntPtr pUnmanagedBytes = new IntPtr(0);
  82. int nLength;
  83. nLength = Convert.ToInt32(fs.Length);
  84. // 读取文件的内容放入数组中.
  85. bytes = br.ReadBytes(nLength);
  86. // 分配一些非托管内存对于那些字节
  87. pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
  88. // 复制管理到非托管数组的字节数组
  89. Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
  90. // 发送到打印机的非托管的字节
  91. bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
  92. // 非托管内存分配
  93. Marshal.FreeCoTaskMem(pUnmanagedBytes);
  94. return bSuccess;
  95. }
  96. public static bool SendStringToPrinter(string szPrinterName, string szString)
  97. {
  98. IntPtr pBytes;
  99. Int32 dwCount;
  100. // 有多少字符在字符串
  101. dwCount = szString.Length;
  102. // 假设打印机期待ANSI的文本,然后将字符串ANSI文本。
  103. pBytes = Marshal.StringToCoTaskMemAnsi(szString);
  104. // 转换后的字符串发送到打印机的ANSI
  105. SendBytesToPrinter(szPrinterName, pBytes, dwCount);
  106. Marshal.FreeCoTaskMem(pBytes);
  107. return true;
  108. }
  109. }
  110. }