[英]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.