【问题标题】:How to close Pdf files from C#如何从 C# 关闭 Pdf 文件
【发布时间】:2012-03-21 12:20:59
【问题描述】:

我尝试从我的 C# 代码在 Adob​​e Reader 中打开 2 个 pdf 文件。让我们称它们为 A 和 B,并且 A 在 B 之前打开。

现在,当我尝试终止与文件 A 关联的进程时,文件 B 也会关闭,因为它们链接到同一个进程。有没有办法在不关闭文件 B 的情况下关闭文件 A。

此外,当我第一次尝试终止与 File B 关联的进程时,什么也没有发生,并且 File B 仍然保持打开状态。

我应该如何解决以上两种情况。

我已经处理了这两个文件。有什么办法可以关闭手柄

【问题讨论】:

  • 发布启动 pdf 并杀死它的代码

标签: c# pdf process kill


【解决方案1】:

在我看来,您应该使用适用于 Acrobat 的应用程序间通信 API,它具有打开和关闭文档的功能。与通过 IAC (pdf documentation here) 获得的相比,您所做的相当不优雅。

【讨论】:

    【解决方案2】:

    您可以通过以下代码找到A的PDF查看器的过程。

    using System.Diagnostics;
    
    public bool FindAndKillProcess(string name)
    {
        //here we're going to get a list of all running processes on
        //the computer
        foreach (Process clsProcess in Process.GetProcesses()) {
            //now we're going to see if any of the running processes
            //match the currently running processes by using the StartsWith Method,
            //this prevents us from incluing the .EXE for the process we're looking for.
            //. Be sure to not
            //add the .exe to the name you provide, i.e: NOTEPAD,
            //not NOTEPAD.EXE or false is always returned even if
            //notepad is running
            if (clsProcess.ProcessName.StartsWith(name))
            {
                //since we found the proccess we now need to use the
                //Kill Method to kill the process. Remember, if you have
                //the process running more than once, say IE open 4
                //times the loop thr way it is now will close all 4,
                //if you want it to just close the first one it finds
                //then add a return; after the Kill
                clsProcess.Kill();
                //process killed, return true
                return true;
            }
        }
        //process not found, return false
        return false;
    }
    

    然后调用上面的方法。

    FindAndKillProcess("AcroRd32.exe");
    

    这样你就可以杀死PDF查看器的进程了。

    【讨论】:

    • 这将杀死它找到的第一个 Acrobat Reader,它将关闭文件 A、文件 B,或两者兼而有之——谁知道呢?此外,对于 OP 而言,比对 Brijesh 而言更重要的是,您如何知道最终用户使用的是 Adob​​e 而不是其他 PDF 查看器?我们真的需要知道 PDF 是如何启动的,才能知道如何杀死它。
    • @brijesh 但这也会关闭文件 b 因为相同的进程与文件 B 相关联。我不希望文件 b 关闭。
    • 好的,我明白了。有一个解决方案,您需要先关闭 A,然后只打开 B。意味着一次只能打开一个 PDF。所以没有问题。
    • 当您在应用程序中创建文件时,您应该保留使用过的文件名,例如,在字符串数组或其他内容中:' untested air code Private _tmpFileList As New ArrayList() Private Function createTempFile () As String Dim filename As String = _ System.IO.Path.GetTempFileName() _tmpFileList.Add(filename) End Function Private Sub cleanupTempFiles() For Each filename As String In _tmpFileLilst Try File.Delete filename ' Catch -- do something此处为 FileNotFound 或 File is open 最后 System.IO.Path.Delete(filename) End Try Next End Sub
    【解决方案3】:

    尝试:
    if (clsProcess.ProcessName.包含(name))

    代替:
    if (clsProcess.ProcessName.StartsWith(name))

    using System.Diagnostics;
    
      public bool FindAndKillProcess(string name)
            {
                foreach (Process clsProcess in Process.GetProcesses())
                {
                    if (clsProcess.ProcessName.Contains(name))
                    {
                        //To know if it works
                        //MessageBox.Show(clsProcess);
                        clsProcess.Kill();
                        return true;
                    }
                }
                //process not found, return false
                return false;
            }
    
    ////// call the function:
    
                FindAndKillProcess("AcroRd32");
    
    ////// if you have been saved all the variables also you can close you main form
    
                FindAndKillProcess("Form_Name");
    

    【讨论】:

      【解决方案4】:

      我认为一种方法是找到该程序的实例并从您的应用程序中关闭它。以下是如何找到窗口并关闭它的示例:http://www.mycsharpcorner.com/Post.aspx?postID=32

      由于您打开了 2 个 Adob​​e 阅读器实例,您需要确定哪个是哪个。您可以按框架中的文本进行搜索。如果您有 spy++(或类似的替代品)的副本,那么使用外部 GUI 组件会变得更加容易,因为您可以找到有关该窗口的很多信息,包括名称、窗口句柄等等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-17
        • 1970-01-01
        • 1970-01-01
        • 2010-09-30
        • 2014-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多