版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/ / 打印word / / word文件路径 / 指定的打印机 public void Printword(string filepath,string printername) /filepath=d:b.doc; /printername = Microsoft XPS Document Writer; try System.Diagnostics.Process p = new System.Diagnostics.Process(); /不现实调用程序窗口,但是对于某些应用无效 p.StartInfo.CreateNoWindow = true; p.StartInfo.Window
2、Style = System.Diagnostics.ProcessWindowStyle.Hidden; /采用操作系统自动识别的模式 p.StartInfo.UseShellExecute = true; /要打印的文件路径 p.StartInfo.FileName = filepath; Help help = new Help(); help.LogMessage(filepath + - + printername); /指定执行的动作,是打印,即print,打开是 open p.StartInfo.Verb = print; /获取当前默认打印机 string defaultPri
3、nter = GetDefaultPrinter(); /将指定的打印机设为默认打印机 SetDefaultPrinter(printername); /开始打印 p.Start(); /等待十秒 p.WaitForExit(10000); /将默认打印机还原 SetDefaultPrinter(defaultPrinter); catch (Exception ex) help.LogMessage(filepath + - + printername + - + ex.Message); DllImport(Winspool.drv, CharSet = CharSet.Auto, Set
4、LastError = true) private static extern bool SetDefaultPrinter(string printerName); DllImport(winspool.drv, CharSet = CharSet.Auto, SetLastError = true) private static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int pcchBuffer); / / 获取默认的打印机 / / static string GetDefaultPrinter() const
5、 int ERROR_FILE_NOT_FOUND = 2; const int ERROR_INSUFFICIENT_BUFFER = 122; int pcchBuffer = 0; if (GetDefaultPrinter(null, ref pcchBuffer) return null; int lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error = ERROR_INSUFFICIENT_BUFFER) StringBuilder pszBuffer = new StringBuilder(pcchBuf
6、fer); if (GetDefaultPrinter(pszBuffer, ref pcchBuffer) return pszBuffer.ToString(); lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error = ERROR_FILE_NOT_FOUND) return null; throw new Win32Exception(Marshal.GetLastWin32Error(); #region /打印页面不会闪动 public void PrintMethodOther(string filepa
7、th,string printername) try object wordFile = filepath; /d:b.doc; object oMissing = Missing.Value; /自定义object类型的布尔值 object oTrue = true; object oFalse = false; object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges; /定义WORD Application相关 Application appWord = new Application(); /WORD程序不可见 appWord
8、.Visible = false; /不弹出警告框 appWord.DisplayAlerts = WdAlertLevel.wdAlertsNone; /先保存默认的打印机 string defaultPrinter = appWord.ActivePrinter; /打开要打印的文件 /如果在其它函数调用中出错(doc为null),处理办法:建立临时文件夹,还要设置服务的权限属性 Document doc = appWord.Documents.Open( ref wordFile, ref oMissing, ref oTrue, ref oFalse, ref oMissing, re
9、f oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); /设置指定的打印机 appWord.ActivePrinter = printername; /Microsoft XPS Document Writer; /打印 doc.PrintOut( ref oTrue, /此处为true,表示后台打印 ref oFalse, ref oMissin
10、g, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing ); /打印完关闭WORD文件 doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing); /还原原来的默认打印机
11、 appWord.ActivePrinter = defaultPrinter; /退出WORD程序 appWord.Quit(ref oMissing, ref oMissing, ref oMissing); doc = null; appWord = null; catch (Exception ex) help.LogMessage(filepath+-+printername+-+ex.Message); / /解决 word调用打印机报错问题,创建一个临时文件夹 / / public bool CreateFolder() bool ifsuccesss = false; try
12、string systempath = System.Environment.GetFolderPath(Environment.SpecialFolder.System); string fullpath = string.Empty; if (FindSystemWidth() = 32) fullpath = systempath + configsystemprofileDesktop; else fullpath = systempath + configsystemprofileDesktop; if (!Directory.Exists(fullpath) Directory.C
13、reateDirectory(fullpath); ifsuccesss = true; catch (Exception ex) ifsuccesss = false; return ifsuccesss; / / 获取系统的位数 / / public string FindSystemWidth() ConnectionOptions oConn = new ConnectionOptions(); System.Management.ManagementScope oMs = new System.Management.ManagementScope(localhost, oConn); System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery(select AddressWidth from Win32_Processor); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery); M
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 售后服务问题处理通知函发送函6篇
- 2026年上海市工商外国语学校公开招聘笔试备考题库及答案详解
- 万载县2026年城区(郊)公办学校选调教师【96人】考试备考题库及答案详解
- 2026重庆三峡油漆股份有限公司招聘5人笔试模拟试题及答案详解
- 2026年铜陵创邑传媒有限公司公开招聘劳务派遣人员考试备考试题及答案详解
- 职业素养和能力中的基本医学常识2026年单招试题及答案
- 2026浙江金华市武义县供销合作社联合社招聘1人笔试备考试题及答案详解
- 2026广西南宁市武鸣区标营新区幼儿园的招聘笔试备考试题及答案详解
- 2026湖南永州市新田县县直事业单位引进急需紧缺专业人才48人考试备考试题及答案详解
- 2026广东深圳市龙岗区耳鼻咽喉医院招聘6人笔试备考试题及答案详解
- 清真食品安全知识培训课件
- 实战网络靶场应用指南(2025版)-安全牛
- 2025年电厂安全教育考试试题(含答案)
- 学堂在线 现代生活美学-花香茶之道 章节测试答案
- 2025年公文写作公文试题及答案
- 科技立项费用管理办法
- T/CAPA 1-2019脂肪注射移植
- 研学旅行概论课程培训课件
- 船东保障和赔偿责任险条款
- 2021年中国邮政储蓄银行综合柜员岗位资格(初级)模拟考试(一)
- 送货单格式模板
评论
0/150
提交评论