【问题标题】:c# Print PDF with Adobe Reader and closec# 使用 Adobe Reader 打印 PDF 并关闭
【发布时间】:2019-06-21 13:04:13
【问题描述】:
我没有找到使用 Acrobat 并关闭 Acrobat Reader 来打印 PDF(例如,从服务器上的“热”文件夹 (FileSystemWatcher) 打印)的好(免费)和简单的解决方案。所以我写了我自己的,我希望它会帮助别人。
(是的,您可以使用旧的免费福昕阅读器版本,但我们使用它时遇到了太多麻烦,有时会卡在内存中而无法打印)
要点是,打印后,文件必须移动到存档目录,但 Adobe 没有关闭。所以我从来不知道它什么时候完成,或者等待 30 多秒然后杀死(如果服务器需要更长的时间并且需要很多时间,那就不太好了)。
【问题讨论】:
标签:
c#
pdf
printing
adobe
【解决方案1】:
这是我的解决方案,我运行进程并等到我的 Adobe 进程的子进程之一显示最近打开的窗口。
感谢 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