C#直接打印word文档=.docx

上传人:小飞机 文档编号:3153735 上传时间:2023-03-11 格式:DOCX 页数:11 大小:38.61KB
返回 下载 相关 举报
C#直接打印word文档=.docx_第1页
第1页 / 共11页
C#直接打印word文档=.docx_第2页
第2页 / 共11页
C#直接打印word文档=.docx_第3页
第3页 / 共11页
C#直接打印word文档=.docx_第4页
第4页 / 共11页
C#直接打印word文档=.docx_第5页
第5页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《C#直接打印word文档=.docx》由会员分享,可在线阅读,更多相关《C#直接打印word文档=.docx(11页珍藏版)》请在三一办公上搜索。

1、C#直接打印word文档=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.

2、Diagnostics.Process; /不现实调用程序窗口,但是对于某些应用无效 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 + - + pri

3、ntername); /指定执行的动作,是打印,即print,打开是 open p.StartInfo.Verb = print; /获取当前默认打印机 string defaultPrinter = GetDefaultPrinter; /将指定的打印机设为默认打印机 SetDefaultPrinter(printername); /开始打印 p.Start; /等待十秒 p.WaitForExit(10000); /将默认打印机还原 SetDefaultPrinter(defaultPrinter); catch (Exception ex) help.LogMessage(filepat

4、h + - + 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 pszBuffer, ref bool int GetDefaultP

5、rinter(StringBuilder 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 (lastWin32

6、Error = 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(Marshal

7、.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.wdDoNotSaveChanges;

8、/定义WORD Application相关 Application appWord = new Application; /WORD程序不可见 appWord.Visible = false; /不弹出警告框 appWord.DisplayAlerts = WdAlertLevel.wdAlertsNone; /先保存默认的打印机 string defaultPrinter = appWord.ActivePrinter; /打开要打印的文件 /如果在其它函数调用中出错(doc为null),处理办法:建立临时文件夹,还要设置服务的权限属性 Document doc = appWord.Docu

9、ments.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 Docume

10、nt 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); / /解决 word调

12、用打印机报错问题,创建一个临时文件夹 / / 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 = systempath

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

14、entScope oMs = new System.Management.ManagementScope(localhost, oConn); System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery(select from Win32_Processor); ManagementObjectSearcher oSearcher = new AddressWidth ManagementObjectSearcher(oMs, oQuery); ManagementObjectCollection oReturnCollection = oSearcher.Get; string addressWidth = null; foreach (ManagementObject oReturnCollection) addressWidth oReturnAddressWidth.ToString; return addressWidth; oReturn in =

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号