直接打印word文檔_第1頁
直接打印word文檔_第2頁
直接打印word文檔_第3頁
直接打印word文檔_第4頁
直接打印word文檔_第5頁
已閱讀5頁,還剩2頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、 / <summary>        / 打印word        / </summary>        / <param name="filepath">word文件路徑</param>        / <param name

2、="printername">指定的打印機</param>        public void Printword(string filepath,string printername)                    /filepath="d:b.doc"  &

3、#160;         /printername = "Microsoft XPS Document Writer"            try                     

4、;       System.Diagnostics.Process p = new System.Diagnostics.Process();                /不現實調用程序窗口,但是對于某些應用無效              &#

5、160; p.StartInfo.CreateNoWindow = true;                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;                /采用操作系統自

6、動識別的模式                p.StartInfo.UseShellExecute = true;                /要打印的文件路徑           &#

7、160;    p.StartInfo.FileName = filepath;                Help help = new Help();                help.LogMessage(filepath + "-"

8、 + printername);                /指定執行的動作,是打印,即print,打開是 open                p.StartInfo.Verb = "print"      &#

9、160;         /獲取當前默認打印機                string defaultPrinter = GetDefaultPrinter();                /將指定的打印機設為默

10、認打印機                SetDefaultPrinter(printername);                /開始打印             

11、   p.Start();                /等待十秒                p.WaitForExit(10000);           

12、60;    /將默認打印機還原                SetDefaultPrinter(defaultPrinter);                        catch (Exc

13、eption ex)                                        help.LogMessage(filepath + "-" + printername + "-&

14、quot; + ex.Message);                            DllImport("Winspool.drv", CharSet = CharSet.Auto, SetLastError = true)        priv

15、ate static extern bool SetDefaultPrinter(string printerName);        DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)                private static extern bool Get

16、DefaultPrinter(StringBuilder pszBuffer, ref int pcchBuffer);        / <summary>        / 獲取默認的打印機        / </summary>        / <returns></r

17、eturns>        static string GetDefaultPrinter()                    const int ERROR_FILE_NOT_FOUND = 2;            co

18、nst int ERROR_INSUFFICIENT_BUFFER = 122;            int pcchBuffer = 0;            if (GetDefaultPrinter(null, ref pcchBuffer)          

19、0;                 return null;                        int lastWin32Error = Marshal.GetLastWin32Error();  

20、;          if (lastWin32Error = ERROR_INSUFFICIENT_BUFFER)                            StringBuilder pszBuffer = new StringBui

21、lder(pcchBuffer);                if (GetDefaultPrinter(pszBuffer, ref pcchBuffer)                         

22、;           return pszBuffer.ToString();                                lastWin32Error = Marshal.Get

23、LastWin32Error();                        if (lastWin32Error = ERROR_FILE_NOT_FOUND)                  

24、0;         return null;                        throw new Win32Exception(Marshal.GetLastWin32Error();         &

25、#160;      #region      /打印頁面不會閃動        public void PrintMethodOther(string filepath,string printername)                   

26、try                            object wordFile = filepath;                /"d:b.doc"&

27、#160;               object oMissing = Missing.Value;                           /自定義object類型的布爾值 

28、0;              object oTrue = true;                object oFalse = false;               &

29、#160;           object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;                              

30、 /定義WORD Application相關                        Application appWord = new Application();                  &

31、#160;          /WORD程序不可見                appWord.Visible = false;                /不彈出警告框   

32、;             appWord.DisplayAlerts = WdAlertLevel.wdAlertsNone;                       /先保存默認的打印機      &#

33、160;         string defaultPrinter = appWord.ActivePrinter;                             /打開要打印的文件    

34、0;           /如果在其它函數調用中出錯(doc為null),處理辦法:建立臨時文件夾,還要設置服務的權限屬性            Document doc = appWord.Documents.Open(              

35、      ref wordFile,                    ref oMissing,                    ref oTrue,

36、60;                   ref oFalse,                    ref oMissing,        &

37、#160;           ref oMissing,                    ref oMissing,               

38、60;    ref oMissing,                    ref oMissing,                    ref oMissing, 

39、60;                  ref oMissing,                    ref oMissing,         

40、;           ref oMissing,                    ref oMissing,                &

41、#160;   ref oMissing,                    ref oMissing);                        &

42、#160;      /設置指定的打印機                appWord.ActivePrinter = printername;                /"Microsoft XPS Document Writer&

43、quot;                               /打印                doc.PrintOut(  

44、                  ref oTrue, /此處為true,表示后臺打印                    ref oFalse,       

45、60;            ref oMissing,                    ref oMissing,               

46、;     ref oMissing,                    ref oMissing,                    ref oMissing, 

47、;                   ref oMissing,                    ref oMissing,        &

48、#160;           ref oMissing,                    ref oMissing,               

49、60;    ref oMissing,                    ref oMissing,                    ref oMissing, 

50、60;                  ref oMissing,                    ref oMissing,         

51、;           ref oMissing,                    ref oMissing                &#

52、160;   );                              /打印完關閉WORD文件                doc.Clos

53、e(ref doNotSaveChanges, ref oMissing, ref oMissing);                              /還原原來的默認打印機           &

54、#160;    appWord.ActivePrinter = defaultPrinter;                              /退出WORD程序         

55、0;      appWord.Quit(ref oMissing, ref oMissing, ref oMissing);                               doc = null;    &

56、#160;           appWord = null;                        catch (Exception ex)           

57、;                 help.LogMessage(filepath+"-"+printername+"-"+ex.Message);                       &#

58、160;     / <summary>        /解決 word調用打印機報錯問題,創建一個臨時文件夾        / </summary>        / <returns></returns>        publ

59、ic bool CreateFolder()                    bool ifsuccesss = false;            try             &

60、#160;              string systempath = System.Environment.GetFolderPath(Environment.SpecialFolder.System);                string fullpath = string.Empty; &

61、#160;              if (FindSystemWidth() = "32")                              &#

62、160;     fullpath = systempath + "configsystemprofileDesktop"                                else    

63、;                                fullpath = systempath + "configsystemprofileDesktop"         &

64、#160;                      if (!Directory.Exists(fullpath)                        

65、60;           Directory.CreateDirectory(fullpath);                                ifsuccesss = true;                       

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論