如何在Web浏览器控件中显示PDF,而不是打开Acrobat Reader [英] How to display PDF in web browser control instead of opening Acrobat Reader

查看:78
本文介绍了如何在Web浏览器控件中显示PDF,而不是打开Acrobat Reader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Acrobat Reader 11.0.3和Windows 7,Visual Studio 2012,并且已经在Manage Addon中启用了Acrobat Reader

i am using Acrobat Reader 11.0.3 and window 7, Visual studio 2012 and already enabled Acrobat Reader in Manage Addon

,并按照 WPF网络浏览器在Adobe Reader窗口中打开PDF文件

尝试两种方法后仍然有错误

still have error after trying two methods

该网页的导航已被取消 即使使用管理员帐户打开WPF程序

Navigation to the webpage was canceled even if using administrator account to open WPF program

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 = System.IO.Path.GetFileName(App.ResourceAssembly.Location);
            var assemblyValueKey = System.IO.Path.GetFileName(System.Windows.Application.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.
            */
        }

        public ReportForm()
        {
            this.formID = "Test Report";
            //base.Initialization();
            InitializeComponent();
            CheckAndFixWebBrowserRenderingEngine();

            String PDFPath = @"C:\Users\LAB-User2\Downloads\Elimination_Methods_in_Polynomial_Computer_Alge(Bookos.org).pdf";
            WebBro1.Navigate("file:///" + PDFPath);

方法2

我还尝试了另一种添加参考windowformintegration并使用winform的UserControl的方法,该方法仅在独立的WPF小项目中有效 当将其集成到我的大项目中时,出现了以上错误

i have also tried another method which add reference windowformintegration and use UserControl of winform and it only work in an independent WPF small project when integrate this in my big project, it got above error

String PDFPath = @"C:\Users\LAB-User2\Downloads\Elimination_Methods_in_Polynomial_Computer_Alge(Bookos.org).pdf";
            //WebBro1.Navigate("file:///" + PDFPath);
            webBrowser1.Navigate("file:///" + PDFPath);

private System.Windows.Forms.WebBrowser webBrowser1;
<WindowsFormsHost Margin="1" x:Name="windowsFormsHost1" Height="600" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="1000"/>

var uc = new UserControl1();
            this.windowsFormsHost1.Child = uc;


application.UI.CreateNewForm("Test Report", new Client.Reporting.Pages2.ClientStatement.ReportForm());

public partial class ReportForm : BaseForm

public void CreateNewForm(string title, BaseForm form)
    mw.CreateNewForm(title, form);


public class BaseMainWindow : MetroWindow
    {
        public virtual void CreateNewForm(string title, BaseForm form)
        {

        }

更新:它要求在集成到大项目中之前允许html文件中的Internet Explorer屏蔽内容和灰屏

<html>
<body>

<object
  classid="clsid:ca8a9780-280d-11cf-a24d-444553540000" id="pdf1" 
  type="application/pdf"
  data="C:\Users\LAB-User2\Downloads\Elimination_Methods_in_Polynomial_Computer_Alge(Bookos.org).pdf"
  style="width: 100%; height: 100%">

<param name="src" "value="C:\Users\LAB-User2\Downloads\Elimination_Methods_in_Polynomial_Computer_Alge(Bookos.org).pdf"></param>

</object>

</body>
</html>

更新:在大型WPF项目中运行代码时,它会显示一个红叉

update: when run your code in big WPF project, it show a red cross

更新:更新html后,它可以在独立的html文件中运行,但是集成到WPF大项目中,则无法显示并且仅显示红叉

String temphtml = "<!-- saved from url=(0016)http://localhost --><body style=\"border: 0; margin: 0; padding: 0\"><object classid=\"clsid:ca8a9780-280d-11cf-a24d-444553540000\" id=\"pdf1\"  type=\"application/pdf\" data=\"02_LAB_LeaveApplication.pdf\" style=\"width: 100%; height: 100%; border: 0; margin: 0; padding: 0\"></object><script>window.onload = function(){ window.focus();pdf1.setActive(); }</script></body>";
            System.IO.File.WriteAllText(@"C:\Users\LAB-User2\Desktop\vpfs-ivpfs-client\iVPFS_Client\Application\bin\Debug\a.html", temphtml);            
            this.WebBro1.Navigate(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "a.html"));

更新,尽管运行从codeproject下载的项目,它显示错误,然后显示pdf,打开时空白几次,然后我将代码和库复制到一个小的WPF项目中并运行,它也出现错误,只显示白色空白 https://docs.google .com/file/d/0Bxs_ao6uuBDUeV8yREVBOExSbW8/edit?usp = sharing

update though running the project downloaded from the codeproject, it show an error and then can show pdf and have a few times blank when open, then i copy codes and library to a a small WPF project and run, it also have error come out and only show white blank https://docs.google.com/file/d/0Bxs_ao6uuBDUeV8yREVBOExSbW8/edit?usp=sharing

推荐答案

您可以生成一个临时HTML文件来嵌入所需的PDF:

You could generate a temp HTML file to embed the desired PDF:

<body>

<object
  classid="clsid:ca8a9780-280d-11cf-a24d-444553540000" id="pdf1" 
  type="application/pdf"
  data="test.pdf"
  style="width: 100%; height: 100%">

<param name="src" "value"="test.pdf"></param>

</object>

</body>

有趣的是,要使其与本地(file://)PDF文档一起使用,我必须同时指定 data 属性和 src 参数.从本地主机通过http提供服务时,仅 data 就足够了.

What's interesting, to make it work with a local (file://) PDF document, I had to specify both data attribute and src param. When it was served via http from localhost, the data alone was just enough.

更新:我不知道为什么这对OP不起作用,所以我已经编写了一个非常基本的 C#WebBrowser项目对此进行了演示.在我的环境(Win8,IE10,Acrobat Reader 11.0.3)中,它运行可靠.

UPDATE: I could not tell why this doesn't work for the OP, so I've cooked up a very basic C# WebBrowser project demo'ing this. It works quite reliably in my environment (Win8, IE10, Acrobat Reader 11.0.3).

更新:我想我理解为什么会这样.您的项目使用WPF,而我的示例使用Winforms.抱歉,我最初没有注意到这一点. WPF WebBrowser具有不同的安全模型,默认情况下它在IE保护模式下运行.

UPDATE: I think I understand why this is happening. Your project uses WPF, while my sample uses Winforms. Sorry, I did not notice this in the first place. WPF WebBrowser has a different security model, it runs in IE Protected Mode by default.

使用WPF WebBrowser,我设法摆脱了提示(请注意<!-- saved from url=(0016)http://localhost -->),但我确实确认Acrobat Reader控件只是不时挂起加载文件:

With WPF WebBrowser, I've managed to get rid of the prompt (note <!-- saved from url=(0016)http://localhost -->), but I do confirm the Acrobat Reader control just hangs loading the file every now and then:

<!-- saved from url=(0016)http://localhost -->

<body style="border: 0; margin: 0; padding: 0">

<object
  classid="clsid:ca8a9780-280d-11cf-a24d-444553540000" id="pdf1" 
  style="width: 100%; height: 100%; border: 0; margin: 0; padding: 0">

  <param name="src" "value"="test.pdf"></param>

</object>

<script>
window.onload = function()
{
  window.focus();
  pdf1.setActive(); 
}
</script>

</body>

已知Acrobat Reader控件具有保护模式的问题.与其尝试寻找解决方法,不如直接在WPF项目中托管Acrobat Reader ActiveX控件可能更容易.这是可以完成的方法:

Acrobat Reader control is known to have issues with Protected Mode. Rather than trying to find a workaround for this, it may be easier to host Acrobat Reader ActiveX control directly in your WPF project. Here's how it can be done:

http://www.codeproject. com/Articles/380019/使用Adobe-Reader-in-a-WPF-app http://hugeonion.com/2009/04/06/displaying-a-pdf-file-in-a-wpf-application/

这篇关于如何在Web浏览器控件中显示PDF,而不是打开Acrobat Reader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆