




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C#直接打印word文档 using using Microsoft.Office.Interop.Word; (通过添加引用-com组件,找office的word组件/ / 打印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.Pr
2、ocess(); /不现实调用程序窗口,但是对于某些应用无效 p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; /采用操作系统自动识别的模式 p.StartInfo.UseShellExecute = true; /要打印的文件路径 p.StartInfo.FileName = filepath; Help help = new Help(); help.LogMessage(filepath + - + printername);
3、 /指定执行的动作,是打印,即print,打开是 open p.StartInfo.Verb = print; /获取当前默认打印机 string defaultPrinter = GetDefaultPrinter(); /将指定的打印机设为默认打印机 SetDefaultPrinter(printername); /开始打印 p.Start(); /等待十秒 p.WaitForExit(10000); /将默认打印机还原 SetDefaultPrinter(defaultPrinter); catch (Exception ex) help.LogMessage(filepath + -
4、+ printername + - + ex.Message); DllImport(Winspool.drv, CharSet = CharSet.Auto, SetLastError = true) private static extern bool SetDefaultPrinter(string printerName); DllImport(winspool.drv, CharSet = CharSet.Auto, SetLastError = true) private static extern bool GetDefaultPrinter(StringBuilder pszB
5、uffer, ref int pcchBuffer); / / 获取默认的打印机 / / static string GetDefaultPrinter() const 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 (lastWin32Er
6、ror = ERROR_INSUFFICIENT_BUFFER) StringBuilder pszBuffer = new StringBuilder(pcchBuffer); if (GetDefaultPrinter(pszBuffer, ref pcchBuffer) return pszBuffer.ToString(); lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error = ERROR_FILE_NOT_FOUND) return null; throw new Win32Exception(Marsh
7、al.GetLastWin32Error(); #region /打印页面不会闪动 public void PrintMethodOther(string filepath,string printername) try object wordFile = filepath; /d:b.doc; object oMissing = Missing.Value; /自定义object类型的布尔值 object oTrue = true; object oFalse = false; object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChange
8、s; /定义WORD Application相关 Application appWord = new Application(); /WORD程序不可见 appWord.Visible = false; /不弹出警告框 appWord.DisplayAlerts = WdAlertLevel.wdAlertsNone; /先保存默认的打印机 string defaultPrinter = appWord.ActivePrinter; /打开要打印的文件 /如果在其它函数调用中出错(doc为null),处理办法:建立临时文件夹,还要设置服务的权限属性 Document doc = appWord
9、.Documents.Open( ref wordFile, ref oMissing, ref oTrue, ref oFalse, ref oMissing, ref 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 D
10、ocument Writer; /打印 doc.PrintOut( ref oTrue, /此处为true,表示后台打印 ref oFalse, 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, ref oMissing ); /
11、打印完关闭WORD文件 doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing); /还原原来的默认打印机 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); / /解决
12、word调用打印机报错问题,创建一个临时文件夹 / / public bool CreateFolder() bool ifsuccesss = false; try string systempath = System.Environment.GetFolderPath(Environment.SpecialFolder.System); string fullpath = string.Empty; if (FindSystemWidth() = 32) fullpath = systempath + configsystemprofileDesktop; else fullpath =
13、systempath + configsystemprofileDesktop; if (!Directory.Exists(fullpath) Directory.CreateDirectory(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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 上学用劳务合同协议
- 租种农村耕地合同协议
- 商铺搭建合同协议
- 学前班班主任下半年工作计划的
- 外包工人合同协议模板
- 小区线路维保合同协议
- 奠基仪式租赁合同协议
- 山东省就业协议合同
- 短期装修仓库合同协议
- 直播兼职人员合同协议
- 2025年北京市海淀区高三一模政治试卷(含答案)
- 2023年江苏凤凰出版传媒集团有限公司招聘笔试模拟试题及答案解析
- 国开电大操作系统 Linux系统使用 实验报告
- 干部选拔任用工作全部系列表格
- 胃癌合并冠心病的护理查房
- 风电行业产品质量先期策划手册
- 社区日间照料中心运营方案
- 二年级下册期末教学质量分析P的课件
- 初中数学北师大七年级下册(2023年新编)综合与实践综合与实践-设计自己的运算程序 王颖
- 可燃气体报警系统安装记录
- 伸臂式焊接变位机设计总体设计和旋转减速器设计毕业设计
评论
0/150
提交评论