星火微课系统客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ZPrintUtils.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Common.system;
  2. using System.Collections.Generic;
  3. using System.Drawing.Printing;
  4. using System.Runtime.InteropServices;
  5. namespace XHWK.WKTool.Utils
  6. {
  7. internal class ZPrintUtils
  8. {
  9. private static PrintDocument fPrintDocument = new PrintDocument();
  10. /// <summary>
  11. /// 获取本机默认打印机名称
  12. /// </summary>
  13. /// <returns></returns>
  14. public static string DefaultPrinter()
  15. {
  16. return fPrintDocument.PrinterSettings.PrinterName;
  17. }
  18. /// <summary>
  19. /// 获取打印机列表
  20. /// </summary>
  21. /// <returns></returns>
  22. public static List<string> GetLocalPrinters()
  23. {
  24. List<string> fPrinters = new List<string>();
  25. try
  26. {
  27. fPrinters.Add(DefaultPrinter()); //默认打印机始终出现在列表的第一项
  28. }
  29. catch (System.Exception ex)
  30. {
  31. LogHelper.WriteErrLog("添加默认打印机" + ex.Message, ex);
  32. }
  33. try
  34. {
  35. foreach (string fPrinterName in PrinterSettings.InstalledPrinters)
  36. {
  37. if (!fPrinters.Contains(fPrinterName))
  38. {
  39. fPrinters.Add(fPrinterName);
  40. }
  41. }
  42. }
  43. catch (System.Exception ex)
  44. {
  45. LogHelper.WriteErrLog("添加其它打印机" + ex.Message, ex);
  46. }
  47. return fPrinters;
  48. }
  49. /// <summary>
  50. /// 调用win api将指定名称的打印机设置为默认打印机
  51. /// </summary>
  52. /// <param name="Name"></param>
  53. /// <returns></returns>
  54. [DllImport("winspool.drv")]
  55. public static extern bool SetDefaultPrinter(string Name);
  56. /// <summary>
  57. /// 打印
  58. /// </summary>
  59. /// <param name="PDFPath">打印文件的路径</param>
  60. /// <param name="PrinterName">打印机的名称</param>
  61. public static void Print(string PDFPath, string PrinterName)
  62. {
  63. //设置默认打印机
  64. SetDefaultPrinter(PrinterName);
  65. System.Diagnostics.Process p = new System.Diagnostics.Process();
  66. //不能启动进程是否显示错误弹窗,如果文件没有打开方式会提示选择文件的打开方式
  67. p.StartInfo.ErrorDialog = true;
  68. //不显示调用程序窗口,但是对于某些应用无效
  69. p.StartInfo.CreateNoWindow = true;
  70. p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  71. //采用操作系统自动识别的模式
  72. p.StartInfo.UseShellExecute = true;
  73. //要打印的文件路径
  74. p.StartInfo.FileName = PDFPath;
  75. //指定执行的动作,是打印,即print,打开是 open
  76. p.StartInfo.Verb = "print";
  77. //开始打印
  78. p.Start();
  79. //等待15秒
  80. p.WaitForExit(15000);
  81. }
  82. }
  83. }