C#直接打印word文档(25页).doc

上传人:1595****071 文档编号:34795881 上传时间:2022-08-18 格式:DOC 页数:29 大小:27.50KB
返回 下载 相关 举报
C#直接打印word文档(25页).doc_第1页
第1页 / 共29页
C#直接打印word文档(25页).doc_第2页
第2页 / 共29页
点击查看更多>>
资源描述

《C#直接打印word文档(25页).doc》由会员分享,可在线阅读,更多相关《C#直接打印word文档(25页).doc(29页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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.Windo

2、wStyle = 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 defaultPr

3、inter = 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, Se

4、tLastError = 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() cons

5、t 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(pcchBu

6、ffer); 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 filep

7、ath,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程序不可见 appWor

8、d.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, r

9、ef 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 oMissi

10、ng, 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 + ; else fullpath = systempath + ; if (!Directory.Exists(fullpath) Directory.CreateDirectory(fullpath); ifsuccesss = true; catch

13、(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.

14、Management.ObjectQuery(select AddressWidth from Win32_Processor); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery); ManagementObjectCollection oReturnCollection = oSearcher.Get(); string addressWidth = null; foreach (ManagementObject oReturn in oReturnCollection) addressWidth = oReturnAddressWidth.ToString(); return addressWidth; 第 29 页-

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 单元课程

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁