Displaying a local PDF file in WPF with WebBrowser-Control

20,663

Just use

webBrowser.Navigate("file:///" + url);
Share:
20,663
chris6523
Author by

chris6523

Updated on April 19, 2020

Comments

  • chris6523
    chris6523 about 4 years

    I'm trying to display a local PDF file in a WebBrowser-Control. I didn't want to use the Adobe-Libraries, because they don't support 64-bit. Now I already have the code to display a PDF, but only if it is not on the local HDD. When I right-clicked on the WebBrowser-Control and displayed the SourceCode of the HTML, I saved it as an HTML-File to check, if the HTML-Code is correctly working. Well, it works.

    My window only consists of a maximized WebControl. I think the problem are the Security Settings of the local Internet Explorer. I read that a custom IInternetSecurityManager could solve the problem, but I don't know how to implement it... :/

    I'm using C# with .NET Framework 4.0

    Here is my code:

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {            
            string url = "C:/test.pdf";
            string html = "<!-- saved from url=(0014)about:internet -->\n<html>\n<body>\n<embed src=\"" + url + "\" width=\"100%\" height=\"100%\"/>\n</body>\n</html>";
            webBrowser.NavigateToString(html); // System.Windows.Controls.WebBrowser
        }
    

    I the "saved from URL" part does only work if I directly open the HTML-Code in IE, so please tell me what to do, to get this code work... Maybe you have a better solution for my problem. Thanks for your help!

    Regards, Chris

  • anthoLB29
    anthoLB29 about 10 years
    In my case, that works on Win7 but if I run my app on Win8.1, the file is being downloaded by IE.
  • chris6523
    chris6523 over 7 years
    You need some kind of PDF plugin (e.g. Adobe Reader) installed, so IE can display the file. Then it will not be downloaded.