[英]c# Print PDF with Adobe Reader and close
我沒有找到使用Acrobat並關閉Acrobat Reader打印PDF(例如,從服務器上的“熱”文件夾(FileSystemWatcher)中)的良好(免費)簡單解決方案。 所以我寫了自己的書,希望對別人有幫助。 (是的,您可以使用舊的免費Foxit Reader版本,但是我們遇到了太多麻煩,有時卡在內存中而不進行打印)
要點是,在打印后,必須將文件移至存檔目錄,但Adobe並未關閉。 所以我從不知道什么時候完成,或者等待30秒鍾以上然后殺死(如果服務器需要更長的時間並且花費很多時間,那還不是很好)。
在這里,我運行我的解決方案,然后運行該流程,並等待直到Adobe Process的子流程之一顯示最近打開的Window。
感謝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.