1

I have a WebBrowser control where I'm showing a local file (htm), this works fine. I also want to set the Encoding to UTF-8. How can I do that?

I tried this solution: webbrowser encoding problem

But the webBrowser.Document is an object which doesn't have Encoding property.

rene
  • 41,474
  • 78
  • 114
  • 152
Tamás Deák
  • 175
  • 1
  • 13
  • https://www.w3schools.com/html/html_charset.asp – ProgrammingLlama May 05 '18 at 10:26
  • @john I know I can read from html file the Encoding, but my webBrowser dont know that. – Tamás Deák May 05 '18 at 10:29
  • *But the webBrowser.Document is an object which doesn't have Encoding property* .That is strange because according to [the documentation it does have that property](https://msdn.microsoft.com/nl-nl/library/system.windows.forms.htmldocument(v=vs.110).aspx) – rene May 05 '18 at 10:29
  • @rene I'm little bit new in this topic. In my MainWindow.xaml file I have In my Code behind: webBrowser.Document is an object. – Tamás Deák May 05 '18 at 10:35
  • @rene And your link to Windows Forms, my Application is WPF app. https://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser(v=vs.110).aspx – Tamás Deák May 05 '18 at 10:38
  • @rene I have to add reference Windows.Forms and I have to cast this object to HtmlDocument and set the Encoding Property? – Tamás Deák May 05 '18 at 10:42
  • 1
    It looks like you need to cast the `Document` to the right interface: https://stackoverflow.com/questions/27833944/where-can-i-find-the-list-of-interfaces-supported-by-the-wpf-webbrowser-document – rene May 05 '18 at 11:24
  • @rene Yea thanks, I found the solution here in Stackoverflow, but thanks! – Tamás Deák May 05 '18 at 11:34

1 Answers1

2

I found the answer here: WPF WebBrowser and special characters like german "umlaute"

Thanks for @BennoDual

static void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    var webBrowser = sender as WebBrowser;
    if(webBrowser == null) {
        return;
    }
    var doc = (IHTMLDocument2)webBrowser.Document;           

    doc.charset = "utf-8";
    webBrowser.Refresh();
}
Tamás Deák
  • 175
  • 1
  • 13
  • 3
    In case you have not reference IHTMLDocument2, you can grab hold of the Document property via using dynamic: dynamic doc = webBrowser.Document; – Tore Aurstad Apr 23 '20 at 12:54