/// <summary> /// 打印接口 /// </summary> /// <param name="jobs"></param> /// <returns></returns> public static bool Printer(List <PrintJobEntity> jobs) { var blResult = false; var strPdfFile = ""; foreach (PrintJobEntity jobEntity in jobs) { try { AdobeReaderPath = CheckeAdobeReader(jobEntity.PrintMethod); strPdfFile = CheckPrinterFileSource(jobEntity.PrintFilePath, jobEntity.PrintJobName); SizeF marginSize = CheckMarginSize(jobEntity); //获取默认打印机 if (string.IsNullOrEmpty(jobEntity.PrinterName)) { jobEntity.PrinterName = PdfHelper.DefaultPrinter(); } //控制打印机打印 if (String.IsNullOrEmpty(strPdfFile) || !File.Exists(strPdfFile)) { throw new Exception("下载Pdf文件资源失败!"); } else { var isStartAdobeMode = false; isStartAdobeMode = !String.IsNullOrEmpty(AdobeReaderPath) && File.Exists(AdobeReaderPath); if (isStartAdobeMode) { SetPaperSize(jobEntity.PrinterName, jobEntity.PrintPageType); } blResult = PdfHelper.QuietPrinter(PdfHelper.LoadPdf(strPdfFile), marginSize.Height, marginSize.Width, jobEntity.PrintJobName, jobEntity.PrinterName, AdobeReaderPath, jobEntity.PrintPageType, isStartAdobeMode); if (!blResult) { throw new Exception("自定义打印失败!"); } } } catch (Exception ex) { throw ex; } } return(blResult); }
public static bool AdobeReaderPrint(string adobePath, string filePath, string printerName) { try { if (string.IsNullOrEmpty(printerName)) { printerName = PdfHelper.DefaultPrinter(); } var acroList = Process.GetProcessesByName("AcroRd32").ToList(); if (acroList.Count <= 0) { hWnd = new IntPtr(0); if (panelAdobe != null) { panelAdobe.Close(); panelAdobe = null; } } if (hWnd == new IntPtr(0)) { // 查杀AcroRd32进程 try { Process.GetProcessesByName("AcroRd32").ToList().ForEach(x => x.Kill()); Thread.Sleep(500); } catch { } } var p = new Process(); var startInfo = new ProcessStartInfo(); startInfo.FileName = adobePath; var argumentone = "/h /t \"" + filePath + "\" \"" + printerName + "\""; if (hWnd != new IntPtr(0) && IsAsyncPrint == false) { argumentone = "/n /t \"" + filePath + "\" \"" + printerName + "\""; } startInfo.Arguments = argumentone; startInfo.CreateNoWindow = true; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.UseShellExecute = false; p.StartInfo = startInfo; var isShellOk = p.Start(); if (isShellOk == false) { return(false); } if (hWnd != new IntPtr(0)) { DateTime sPrintTime = System.DateTime.Now; //智能检测一下打印机是否可以进入异步打印状态 isShellOk = p.WaitForExit(30000); if (System.DateTime.Now.AddSeconds(-3) > sPrintTime) { IsAsyncPrint = false; } else { IsAsyncPrint = true; } } if (isShellOk == false) { throw new Exception("打印任务入Windows打印机等待队列超时!"); } if (hWnd == new IntPtr(0)) { var thread = new Thread(() => { var whileCount = 0; while (true && whileCount++ < 20) { var prc = Process.GetProcessesByName("AcroRd32"); foreach (var item in prc) { if (item.MainWindowHandle != hWnd) { hWnd = item.MainWindowHandle; break; } } if (hWnd != new IntPtr(0)) { break; } Thread.Sleep(300); } }); thread.IsBackground = true; thread.Start(); thread.Join(); if (panelAdobe == null && hWnd != new IntPtr(0)) { panelAdobe = new AdobePanel(); var insertwin = new FillAdobeWindow(panelAdobe.GetAdobePanel, hWnd); panelAdobe.Hide(); } // 检查Adobe Pdf 默认弹窗提示. AdobeDialogueUI.AdobeDefaultPDF_Dialogue(); } return(true); } catch (Exception ex) { throw new Exception("调用Adobe Reader组件出现异常", ex); } }