WPF WebBrowser cannot open local .pdf files OR complex URLs.

Stout 286 Reputation points
2021-07-11T09:03:46.727+00:00

Hi. I have a <WebBrowser> control in the WPF app, and it should either display a local PDF file or render a webpage URL inside.

There is a problem with both requirements.

1) Local PDF files: When I navigate to the local PDF file location, it prompts me to download the file, rather than display it on the UI.

2) Webpage URL: When I set the source to a URL, e.g. http://www.microsoft.com, it uses Internet Explorer and gets hiccups with all scripts. Error message: "Script Error | An error has occurred in the script on this page. Do you want to continue running scripts on this page?"

The page gets displayed fine, however. It just gives me those Script Error Yes/No messageboxes.

Is there a way I can fix that? If not, any alternatives?

Thanks.

Code:

            <WebBrowser x:Name="webBrowser" />


            if (Uri.IsWellFormedUriString(value, UriKind.Absolute)) // e.g. http://www.microsoft.com
            {
                webBrowser.Source = new Uri(HelpFilesCombobox.SelectedValue.ToString());
            }
            else // e.g. C:\MyFile.pdf
            {
                webBrowser.Navigate("file:///" + HelpFilesCombobox.SelectedValue.ToString());
            }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,777 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 85,211 Reputation points
    2021-07-11T09:32:08.133+00:00

    You need to update registry values, like "EditFlags" or FEATURE_BROWSER_EMULATION
    Otherwise, you should use the WebView2 control

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.