How to display PDF in web browser control instead of opening Acrobat Reader

18,234

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>

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.

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).

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.

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 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/Using-Adobe-Reader-in-a-WPF-app http://hugeonion.com/2009/04/06/displaying-a-pdf-file-within-a-wpf-application/

Share:
18,234
ControlPoly
Author by

ControlPoly

Updated on June 04, 2022

Comments

  • ControlPoly
    ControlPoly about 2 years

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

    and follow WPF webbrowser opens PDF file in Adobe reader window

    still have error after trying two methods

    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);
    

    Method 2

    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)
            {
    
            }
    

    update: it ask to allow block content and grey screen in internet explorer in html file before integrate into big project

    <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>
    

    enter image description here

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

    update : after updated html it works in independent html file, however integrated in WPF big project, it failed to show and only show a red cross

    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"));
    

    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

  • ControlPoly
    ControlPoly almost 11 years
    it become grey screen after try in a html file, where is wrong?
  • noseratio
    noseratio almost 11 years
    Could you confirm the .PDF and .HTML are in the same folder? The above is tested to work for me under Win8, IE10 and Acrobat Reader 11.0.3.
  • ControlPoly
    ControlPoly almost 11 years
    i confirm that pdf and html are in the same folder
  • ControlPoly
    ControlPoly almost 11 years
    this file has only one page
  • ControlPoly
    ControlPoly almost 11 years
    sometimes it do not have initializing, and only grey, even if it has initializing diagram, it just stopped
  • noseratio
    noseratio almost 11 years
    I've updated my answer with a working C# project, try compiling and running it.
  • ControlPoly
    ControlPoly almost 11 years
    the link i run said can not find goo.gl
  • ControlPoly
    ControlPoly almost 11 years
    i can run your project and success to show pdf, however can not show our big project, it show a red cross at left hand corner
  • ControlPoly
    ControlPoly almost 11 years
    still the same red cross, it do not ask for yes or no for security after integrated to big project, it only ask when i test in a separated html file
  • noseratio
    noseratio almost 11 years
    Unlike with WinForms, using Acrobat Reader ActiveX inside WPF WebBrowser appears to be unreliable. As I've suggested above, consider hosting Acrobat Reader ActiveX directly in your WPF form, using the links provided. It really is not a lot of work. I can't help any further, sorry.
  • ControlPoly
    ControlPoly almost 11 years
    i find no acrobat reader in com component, what should do next?
  • noseratio
    noseratio almost 11 years
    Is the following key present in your registry: HKEY_CLASSES_ROOT\AcroPDF.PDF.1\CLSID? If it is, you have the required component. What to do next: visit the links I mentioned and watch this helpful screencast by Christopher Hujanen, from the second link. It really has everything you need.
  • ControlPoly
    ControlPoly almost 11 years
    after install reader 8 and find it in common file, got error After install Arocbat reader 8 and find the com component in Common file, Cannot use Adobe Reader to view PDF in your web browser. Reader will now exit
  • ControlPoly
    ControlPoly almost 11 years
    i uploaded the project, it only show blank page docs.google.com/file/d/0Bxs_ao6uuBDUeV8yREVBOExSbW8/…
  • anthoLB29
    anthoLB29 about 10 years
    @Noseratio It seems that your app does not display big PDF (27 Mo in my case)
  • noseratio
    noseratio about 10 years
    @anthoLB29, this is not my app. It's Acrobat Reader ActiveX control.
  • anthoLB29
    anthoLB29 about 10 years
    I found my issue, when the app is run as administrative, the pdf file won't be displayed in anyway ...
  • Vojtěch Dohnal
    Vojtěch Dohnal about 10 years
    The problem with displaying ActiveX PDF in WPF is, that when Adobe Reader add-on is disabled in IE, it does not work.
  • reckface
    reckface almost 6 years
    the change from <embed /> to <object /> did it for me. It revealed a certificate error that was easy to fix. <embed /> didn't show any errors, it just hung.