WPF WebBrowser在Adobe Reader窗口中打开PDF文件

3

我正在创建一个32位的WPF应用程序。它需要在WebBrowser控件中显示创建的PDF文件。

"WebBrowser.Navigate(new Url("D:\\TestPDF\\MyDocument.pdf"))";

它在Adobe Reader窗口中打开PDF文件。

我的需求是PDF应该在WebBrowser内部打开,而不是在Adobe Reader窗口中打开。 我也尝试了WebBrowser.NavigateToStreamWebBrowser.Source,但它们都不起作用。

有什么解决办法吗?期待您的帮助。

1个回答

4

您是否检查过在Internet Explorer中安装了Adobe Reader?您还应该验证您的Internet Explorer是否允许打开嵌入式PDF文件。

有时候,使用另一个Internet Explorer渲染引擎可以解决问题。这可以通过以下代码实现(请注意:需要管理员权限)。

private void CheckAndFixWebBrowserRenderingEngine()
{
    RegistryKey baseRegistryKey = Registry.LocalMachine;
    string renderingEngineSubKeyString = @"SOFTWARE";

    // 64bit operationg systems have another registry path
    if (Environment.Is64BitOperatingSystem)
    {
        renderingEngineSubKeyString += @"\Wow6432Node";
    }

    renderingEngineSubKeyString += @"\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";

    var assemblyValueKey = Path.GetFileName(App.ResourceAssembly.Location);
    var renderingEngingeValue = 9999; // check other values below

    try
    {
        RegistryKey sk1 = baseRegistryKey.CreateSubKey(renderingEngineSubKeyString);

        var value = sk1.GetValue(assemblyValueKey);
        if (value == null || value.ToString() != renderingEngingeValue.ToString())
        {
            sk1.SetValue(assemblyValueKey, renderingEngingeValue);

            LogHandler.Instance.Add(string.Format("Did update webbrowser rendering engine from {0} to 9000.", value == null ? "[missing]" : value));
        }
    }
    catch (Exception ex)
    {
        LogHandler.Instance.Add("Could not check webbrowser rendering engine in registry.");
        LogHandler.Instance.Add(ex.ToString(), Logging.LoggingPriorities.Exception);
    }

    /*
    9999 (0x270F) 
    Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.

    9000 (0x2328) 
    Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.

    8888 (0x22B8) 
    Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.

    8000 (0x1F40) 
    Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.

    7000 (0x1B58) 
    Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.
    */
}

1
我发现了一个简单的事情,可以使用互操作性。使用Adobe Reader添加用户控件(Winform),并将其放入WPF中使用互操作性。 - nullrefereceexception
@Bernhard Krenz,这段代码适用于32位的Windows系统,但在64位版本上无法正常工作。 - Muhammad Hani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接