星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Common.system;
  2. using Microsoft.Win32;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Drawing.Printing;
  6. using System.Runtime.InteropServices;
  7. namespace XHWK.WKTool.Utils
  8. {
  9. internal class ZPrintUtils
  10. {
  11. private static readonly PrintDocument FPrintDocument = new PrintDocument();
  12. /// <summary>
  13. /// 获取本机默认打印机名称
  14. /// </summary>
  15. /// <returns></returns>
  16. public static string DefaultPrinter()
  17. {
  18. return FPrintDocument.PrinterSettings.PrinterName;
  19. }
  20. /// <summary>
  21. /// 获取打印机列表
  22. /// </summary>
  23. /// <returns></returns>
  24. public static List<string> GetLocalPrinters()
  25. {
  26. List<string> fPrinters = new List<string>();
  27. try
  28. {
  29. fPrinters.Add(DefaultPrinter()); //默认打印机始终出现在列表的第一项
  30. }
  31. catch (System.Exception ex)
  32. {
  33. LogHelper.WriteErrLog("添加默认打印机" + ex.Message, ex);
  34. }
  35. try
  36. {
  37. foreach (string fPrinterName in PrinterSettings.InstalledPrinters)
  38. {
  39. if (!fPrinters.Contains(fPrinterName))
  40. {
  41. fPrinters.Add(fPrinterName);
  42. }
  43. }
  44. }
  45. catch (System.Exception ex)
  46. {
  47. LogHelper.WriteErrLog("添加其它打印机" + ex.Message, ex);
  48. }
  49. return fPrinters;
  50. }
  51. /// <summary>
  52. /// 调用win api将指定名称的打印机设置为默认打印机
  53. /// </summary>
  54. /// <param name="name"></param>
  55. /// <returns></returns>
  56. [DllImport("winspool.drv")]
  57. public static extern bool SetDefaultPrinter(string name);
  58. /// <summary>
  59. /// 打印
  60. /// </summary>
  61. /// <param name="pdfPath">打印文件的路径</param>
  62. /// <param name="printerName">打印机的名称</param>
  63. public static void Print(string pdfPath, string printerName)
  64. {
  65. //设置默认打印机
  66. SetDefaultPrinter(printerName);
  67. Process p = new Process();
  68. //不能启动进程是否显示错误弹窗,如果文件没有打开方式会提示选择文件的打开方式
  69. p.StartInfo.ErrorDialog = true;
  70. //不显示调用程序窗口,但是对于某些应用无效
  71. p.StartInfo.CreateNoWindow = true;
  72. p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  73. //采用操作系统自动识别的模式
  74. p.StartInfo.UseShellExecute = true;
  75. //要打印的文件路径
  76. p.StartInfo.FileName = pdfPath;
  77. //指定执行的动作,是打印,即print,打开是 open
  78. p.StartInfo.Verb = "print";
  79. //开始打印
  80. p.Start();
  81. //等待15秒
  82. p.WaitForExit(15000);
  83. }
  84. /// <summary>
  85. /// 是否安装AcroRd32
  86. /// </summary>
  87. /// <returns></returns>
  88. public static bool IsExistAcroRd32()
  89. {
  90. RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe");
  91. return key != null;
  92. }
  93. public static bool Print2(string pdfPath)
  94. {
  95. try
  96. {
  97. RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe");
  98. if (key != null)
  99. {
  100. string acroRd32Path = key.GetValue("").ToString();
  101. string args = $"/P \"{pdfPath}\"";
  102. Process.Start(acroRd32Path, args);
  103. return true;
  104. }
  105. return false;
  106. }
  107. catch
  108. {
  109. // ignored
  110. }
  111. return false;
  112. }
  113. public static void InstallAcroRd32ByUser()
  114. {
  115. string downloadUrl = "https://xhkjedu.oss-cn-huhehaote.aliyuncs.com/runtime/AcroRdrDC1900820071_zh_CN.exe";
  116. Process.Start(downloadUrl);
  117. }
  118. }
  119. }