星火微课系统客户端
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 2.6KB

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