版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年地质灾难与城市规划的协调发展
- 2025年广州事业单位招考试题及答案
- 2025年昌平事业单位财务考试题及答案
- 2026年绿色建筑的流体力学设计原则
- 2025年心理科护士招聘笔试试题及答案
- 2025年经济学保研专业笔试真题及答案
- 2025年埭溪水务事业单位招聘考试及答案
- 2025年南京公务员事业单位考试及答案
- 2026河南中原再担保集团科技融资担保有限公司招聘4人笔试备考题库及答案解析
- 2026年丹阳市卫生健康委员会所属事业单位公开招聘工作人员101人考试参考题库及答案解析
- 自来水维修抢修知识培训课件
- 2025浙江绍兴市新闻传媒中心(传媒集团)招聘6人笔试题库历年考点版附带答案详解
- 第四单元民族关系与国家关系(任务型复习课件)历史统编版选择性必修1
- 20kV及以下配电网工程设备材料价格信息(2025年上半年)
- 铁科院试验员培训课件
- 2023-2024学年湖南长沙雨花区七年级上册语文期末试卷及答案
- 实验室2024年管理评审资料完整版(含内审报告)符合新版《评审准则》
- 5年级下册英语人教版单词表
- 腊味宣传课件及教案
- 贵州省仓储物流管理办法
- 2025年湖北省中考数学试卷
评论
0/150
提交评论