【问题标题】:Is there Functionality Is there print pdf file multi request C#是否有功能是否有打印pdf文件多请求C#
【发布时间】:2021-08-03 10:50:10
【问题描述】:

大家好,我的情况是我试图从控制台应用程序打印 PDF 文件,它是文件观察程序。 实际上,一旦在该文件夹中创建了 Xml 文件,我就会将 Xml 放在特定的物理路径中。文件观察器操作启动。 读取打印机名称和打印文件等 XML 内容。基于发送到打印机的那个文件。 我的问题是,如果我给多个请求,第一个或两个文件没有发送到打印机队列。立即给另一个多个请求,所有文件都发送到打印机队列。 15 或 30 分钟后出现相同的问题,第一个或两个文件未发送到打印机。 我的代码,

 public  static void FileWatcherfunction()
        {
            string strprintpath = ConfigurationManager.AppSettings["Schedulepath"];
            strprintpath = strprintpath + @"\print";
            //Console.WriteLine("config path:: "+ strprintpath);
            Logger.Log("Print path : "+ strprintpath);
            _fileWatcher = new FileSystemWatcher(strprintpath);
           
            _fileWatcher.Created += new FileSystemEventHandler(_fileWatcher_Created);
            _fileWatcher.Deleted += new FileSystemEventHandler(_fileWatcher_Deleted);

           }
 private static void _fileWatcher_Created(object sender, FileSystemEventArgs e)
 {
    Printfile(strPrinterName, strPrintFileName);
 }
  private static void Printfile(String strPrinterName, String strPrintFileName)
   {
    if (File.Exists(strPrintFileName))
                {
                    Process p = new Process();
                    try
                    {
                        
                        strFileType = strPrintFileName.Substring(strPrintFileName.Length - 3);
                        Logger.Log("strFileType:" + strFileType);
                        p.StartInfo.FileName = strPrintFileName;
                        p.StartInfo.Verb = "PrintTo";
                        p.StartInfo.UseShellExecute = true;
                        p.StartInfo.CreateNoWindow = true;
                        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        p.StartInfo.Arguments = "\"" + strPrinterName + "\"";
                        p.Start();

                        iStartProcId = p.Id;
                        Logger.Log("iStartProcId:"+ iStartProcId) ;
                        p.WaitForExit(15000);
                        Logger.Log("This document has been sent to the printer : " + strPrinterName);
                        p.EnableRaisingEvents = true;
                       // p.WaitForInputIdle();
                        if (strFileType.ToUpper() =="PDF")
                        {
                            Logger.Log("FileType is PDF: ");
                            if (!p.HasExited)
                            {
                                try
                                {
                                    KillAdobe("AcroRd32", iStartProcId);
                                }
                                catch (Exception killex)
                                {
                                }
                                p.CloseMainWindow();
                                p.Close();
                                
                            }
                           

                        }

                        Logger.Log("Printing process completed");
                    }

                    catch (Exception exp)
                    {
                        KillAdobe("AcroRd32", iStartProcId);
                        Logger.Log("Error: Exception: " + exp.Message);

                    }
        }
    }

【问题讨论】:

    标签: c# printing network-printers


    【解决方案1】:

    xml 文件是如何创建和处理的?请注意,FileSystemWatcher.Created 事件并不一定意味着您的 xml 文件是完整的。来自docs

    创建文件后立即引发 OnCreated 事件。如果正在将文件复制或传输到监视目录,则将立即引发 OnCreated 事件,然后引发一个或多个 OnChanged 事件。

    其他一些评论:

    • 获取带有Path.GetExtension的文件扩展名
    • 将扩展名与String.Equals(string, string, StringComparison)StringComparison.OrdinalIgnoreCase 进行比较。 String.ToUpper 创建一个新字符串 - 你不需要它
    • 15 秒打印文档有时可能不够。我会等待更长的时间。另一个陷阱可能是 acrord32.exe 不会立即退出,它可能会停留在后台等待其他操作。也许你永远不应该终止进程,你不检查退出代码
    • 请注意,在终端服务器上,您也可以从其他会话中获取一大堆 acrord32.exe 进程,具体取决于您的权限。在这种情况下,按名称杀死进程可能是一个问题 - 如果您的 KillAdobe 方法这样做
    • Process.CloseMainWindow 不会在没有任何用户界面的情况下结束进程,或者如果进程不再对窗口消息做出反应并且也可能引发异常
    • Process.HasExited 并不一定意味着,打印过程已经完成。也可能是,新启动的应用程序将请求传达给已运行的实例并在打印完成之前退出
    • FileSystemWatcher 也可能成为问题,请参阅 Error 事件

    【讨论】:

    猜你喜欢
    • 2012-08-26
    • 2020-06-26
    • 1970-01-01
    • 2020-04-21
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    相关资源
    最近更新 更多