WPF Webbrowser How do I zoom out on Page loaded event?

0

This Event handler returns:

System.Runtime.InteropServices.COMException: 'Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))'

Code:

private void Browser_Loaded(object sender, NavigationEventArgs e)
    {
        browser.InvokeScript("Document.Body.Style =" + "zoom:80%");
    }

i have tried many solutions found online, but none seem to work for me please advise

wpf
webbrowser-control
asked on Stack Overflow Mar 2, 2019 by Chen Zen • edited Mar 4, 2019 by halfer

1 Answer

0

Try this

private void Browser_Loaded(object sender, NavigationEventArgs e)
    {
        var zoom = 0.8;
        mshtml.IHTMLDocument2 doc = browser.Document as mshtml.IHTMLDocument2;
        doc.parentWindow.execScript("document.body.style.zoom=" + zoom.ToString().Replace(",", ".") + ";");
    }

Its working for me.

Don't forget to reference Microsoft.mshtml

References:

answered on Stack Overflow Mar 2, 2019 by zeeshan

User contributions licensed under CC BY-SA 3.0