简体   繁体   English

c#使用Adobe Reader打印PDF并关闭

[英]c# Print PDF with Adobe Reader and close

I didnt found a good (free) and simple Solution for printing PDFs (for eg from a "Hot"-Folder (FileSystemWatcher) on a Server) with Acrobat and close Acrobat Reader. 我没有找到使用Acrobat并关闭Acrobat Reader打印PDF(例如,从服务器上的“热”文件夹(FileSystemWatcher)中)的良好(免费)简单解决方案。 So i wrote my own and i hope it will help someone. 所以我写了自己的书,希望对别人有帮助。 (Yes, u can use a old free Foxit Reader Version, but we had too much trouble with it, sometimes stuck in Memory without printing) (是的,您可以使用旧的免费Foxit Reader版本,但是我们遇到了太多麻烦,有时卡在内存中而不进行打印)

The Point was, after printing, the file must be moved to a archive dir, but Adobe did not close. 要点是,在打印后,必须将文件移至存档目录,但Adobe并未关闭。 So i never knowed when its done, or wait 30+ Seconds and kill (not so fine if the Server needs longer and takes to much time). 所以我从不知道什么时候完成,或者等待30秒钟以上然后杀死(如果服务器需要更长的时间并且花费很多时间,那还不是很好)。

Here my Solution, i run the Process and wait until one of the subprocesses of my Adobe Process shows the recent open Window. 在这里,我运行我的解决方案,然后运行该流程,并等待直到Adobe Process的子流程之一显示最近打开的Window。

Thanks to mtijn for his "Process Searcher" solution https://stackoverflow.com/a/7189381/480982 感谢mtijn提供的“ Process Searcher”解决方案https://stackoverflow.com/a/7189381/480982

var prz = Process.Start("C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe", "/h /t \"" + YOURPDFFILE + "\" \"" + YOURPRINTER + "\"");

            bool loop = true;
            while (loop)
            {
//u can use Thread.Sleep(x) too;
                prz.WaitForExit(500);

                ManagementObjectSearcher searcher = new ManagementObjectSearcher(
       "SELECT * " +
       "FROM Win32_Process " +
       "WHERE ParentProcessId=" + prz.Id);
                ManagementObjectCollection collection = searcher.Get();
                if (collection.Count > 0)
                {

                    foreach (var item in collection)
                    {
                        UInt32 childProcessId = (UInt32)item["ProcessId"];
                        if ((int)childProcessId != Process.GetCurrentProcess().Id)
                        {

                            Process childProcess = Process.GetProcessById((int)childProcessId);

//If a File is open the Title begins with "Filename - Adobe ...", but after print/closing the recent window starts with "Adobe Acr..."
           if(childProcess.MainWindowTitle.StartsWith("Adobe Acrobat"))
                                {
                                loop = false;
                                break;
                            }


                        }
                    }
                }


            }



           //"Recent" Window found, lets kill the Process
            prz.Kill();


// Now here u can move or Delete the pdf file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C# 中不使用 adobe reader 打印 pdf 文件? - how to print a pdf file without using adobe reader in C#? 如何在 C# 中使用 Adobe 阅读器以彩色打印 pdf 文件 - How to print a pdf file in color using Adobe reader in C# Adobe PDF reader com组件C#在特定页码上打开 - Adobe PDF reader com component C# open on specific pagenumber 保存由 c# 中的进程在 Adobe Reader 中打开的 pdf 文件 - Saving a pdf file opened in Adobe Reader by a process in c# 使用Windows 7 Adob​​e Reader在PDF中显示FormFields的值? (iText,C#创建的PDF) - Display values of FormFields in PDF using Windows 7 Adobe Reader? (iText, C# created PDF) 使用Foxit Reader和C#打印PDF文件 - Print PDF file using Foxit Reader and C# 无法使用C#代码在Adobe Reader 11中的特定页面上打开pdf文件 - unable to open a pdf file at specific page in Adobe Reader 11 using C# code 使用 Process.Start 从 c# 打开 pdf:Adobe Reader 在 3 秒后关闭 - Open pdf from c# with Process.Start: Adobe Reader closes after 3 seconds ECDSA 签名的 PDF 无法使用 iText 7 (C#) 进行签名验证,但使用 Adobe Reader DC 成功 - ECDSA signed PDF fails signature verification with iText 7 (C#), but succeeds with Adobe Reader DC C#在尝试打印文档时防止出现 Adob​​e Reader 窗口 - C# Prevent Adobe Reader Window from coming up when trying to print a document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM