using Common.system; using Microsoft.Win32; using System.Collections.Generic; using System.Diagnostics; using System.Drawing.Printing; using System.Runtime.InteropServices; namespace XHWK.WKTool.Utils { internal class ZPrintUtils { private static readonly PrintDocument FPrintDocument = new PrintDocument(); /// /// 获取本机默认打印机名称 /// /// public static string DefaultPrinter() { return FPrintDocument.PrinterSettings.PrinterName; } /// /// 获取打印机列表 /// /// public static List GetLocalPrinters() { List fPrinters = new List(); try { fPrinters.Add(DefaultPrinter()); //默认打印机始终出现在列表的第一项 } catch (System.Exception ex) { LogHelper.WriteErrLog("添加默认打印机" + ex.Message, ex); } try { foreach (string fPrinterName in PrinterSettings.InstalledPrinters) { if (!fPrinters.Contains(fPrinterName)) { fPrinters.Add(fPrinterName); } } } catch (System.Exception ex) { LogHelper.WriteErrLog("添加其它打印机" + ex.Message, ex); } return fPrinters; } /// /// 调用win api将指定名称的打印机设置为默认打印机 /// /// /// [DllImport("winspool.drv")] public static extern bool SetDefaultPrinter(string name); /// /// 打印 /// /// 打印文件的路径 /// 打印机的名称 public static void Print(string pdfPath, string printerName) { //设置默认打印机 SetDefaultPrinter(printerName); Process p = new Process(); //不能启动进程是否显示错误弹窗,如果文件没有打开方式会提示选择文件的打开方式 p.StartInfo.ErrorDialog = true; //不显示调用程序窗口,但是对于某些应用无效 p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //采用操作系统自动识别的模式 p.StartInfo.UseShellExecute = true; //要打印的文件路径 p.StartInfo.FileName = pdfPath; //指定执行的动作,是打印,即print,打开是 open p.StartInfo.Verb = "print"; //开始打印 p.Start(); //等待15秒 p.WaitForExit(15000); } /// /// 是否安装AcroRd32 /// /// public static bool IsExistAcroRd32() { RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe"); return key != null; } public static bool Print2(string pdfPath) { try { RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe"); if (key != null) { string acroRd32Path = key.GetValue("").ToString(); string args = $"/P \"{pdfPath}\""; Process.Start(acroRd32Path, args); return true; } return false; } catch { // ignored } return false; } public static void InstallAcroRd32ByUser() { string downloadUrl = "https://xhkjedu.oss-cn-huhehaote.aliyuncs.com/runtime/AcroRdrDC1900820071_zh_CN.exe"; Process.Start(downloadUrl); } } }