博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 使用 Windows API 发送文件到打印机
阅读量:6280 次
发布时间:2019-06-22

本文共 7313 字,大约阅读时间需要 24 分钟。

最近需要做一个打印的功能,于是在网上找到了这么一个方法。

  [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;    }    public class PrintCode    {        [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);        ///         /// 该方法把非托管内存中的字节数组发送到打印机的打印队列        ///         /// 打印机名称        /// 非托管内存指针        /// 字节数        /// 
成功返回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"; try { // 打开打印机 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); } if (bSuccess == false) { dwError = Marshal.GetLastWin32Error(); } } catch (Win32Exception ex) { WriteLog(ex.Message); bSuccess = false; } return bSuccess; } /// /// 发送文件到打印机方法 /// /// 打印机名称 /// 打印文件的路径 ///
public static bool SendFileToPrinter(string szPrinterName, string szFileName) { bool bSuccess = false; try { // 打开文件 FileStream fs = new FileStream(szFileName, FileMode.Open); // 将文件内容读作二进制 BinaryReader br = new BinaryReader(fs); // 定义字节数组 Byte[] bytes = new Byte[fs.Length]; // 非托管指针 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); fs.Close(); fs.Dispose(); } catch (Win32Exception ex) { WriteLog(ex.Message); bSuccess = false; } return bSuccess; } /// /// 将字符串发送到打印机方法 /// /// 打印机名称 /// 打印的字符串 ///
public static bool SendStringToPrinter(string szPrinterName, string szString) { bool flag = false; try { IntPtr pBytes; Int32 dwCount; // 获取字符串长度 dwCount = szString.Length; // 将字符串复制到非托管 COM 任务分配的内存非托管内存块,并转换为 ANSI 文本 pBytes = Marshal.StringToCoTaskMemAnsi(szString); // 将已转换的 ANSI 字符串发送到打印机 flag = SendBytesToPrinter(szPrinterName, pBytes, dwCount); // 释放先前分配的非托管内存 Marshal.FreeCoTaskMem(pBytes); } catch (Win32Exception ex) { WriteLog(ex.Message); flag = false; } return flag; } /// /// 写入日志方法 /// /// 记录信息 public static void WriteLog(string msg) { string str = string.Empty; string path = AppDomain.CurrentDomain.BaseDirectory + "log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; FileStream filestream = new FileStream(path, FileMode.OpenOrCreate); str += "************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "************\r\n"; str += msg; str += "************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "************\r\n"; FileStream fs = new FileStream(path, FileMode.Append); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); sw.WriteLine(str); sw.Flush(); sw.Close(); sw.Dispose(); fs.Close(); fs.Dispose(); } }

以上就是全部代码了,调用就很简单了,方法如下:

private void Print_Click(object sender, EventArgs e)        {            //打印文件的路径,该方法获取到的文件路径在应用程序 bin\Debug\ 目录下,所以 ../../ 向上两级目录            string fileName = AppDomain.CurrentDomain.BaseDirectory + @"../../File/demo.txt";            if (PrintCode.SendFileToPrinter("PrinterName", fileName))            {                MessageBox.Show("文件已成功发送至打印队列!","提示信息");            }        }

 

转载于:https://www.cnblogs.com/Brambling/p/6916115.html

你可能感兴趣的文章
js实现页面跳转的几种方式
查看>>
sbt笔记一 hello-sbt
查看>>
常用链接
查看>>
pitfall override private method
查看>>
!important 和 * ----hack
查看>>
聊天界面图文混排
查看>>
控件的拖动
查看>>
svn eclipse unable to load default svn client的解决办法
查看>>
Android.mk 文件语法详解
查看>>
QT liunx 工具下载
查看>>
内核源码树
查看>>
Java 5 特性 Instrumentation 实践
查看>>
AppScan使用
查看>>
Java NIO框架Netty教程(三) 字符串消息收发(转)
查看>>
Ucenter 会员同步登录通讯原理
查看>>
php--------获取当前时间、时间戳
查看>>
Spring MVC中文文档翻译发布
查看>>
docker centos环境部署tomcat
查看>>
JavaScript 基础(九): 条件 语句
查看>>
Linux系统固定IP配置
查看>>