123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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();
-
- /// <summary>
- /// 获取本机默认打印机名称
- /// </summary>
- /// <returns></returns>
- public static string DefaultPrinter()
- {
- return FPrintDocument.PrinterSettings.PrinterName;
- }
-
- /// <summary>
- /// 获取打印机列表
- /// </summary>
- /// <returns></returns>
- public static List<string> GetLocalPrinters()
- {
- List<string> fPrinters = new List<string>();
- 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;
- }
-
- /// <summary>
- /// 调用win api将指定名称的打印机设置为默认打印机
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- [DllImport("winspool.drv")]
- public static extern bool SetDefaultPrinter(string name);
-
- /// <summary>
- /// 打印
- /// </summary>
- /// <param name="pdfPath">打印文件的路径</param>
- /// <param name="printerName">打印机的名称</param>
- 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);
- }
-
- /// <summary>
- /// 是否安装AcroRd32
- /// </summary>
- /// <returns></returns>
- 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);
- }
- }
- }
|