Windows.表格.WebBrowser正在加载带有本地SVG文件的页面

Windows.Forms.WebBrowser loading page with local SVG file

本文关键字:SVG 文件 WebBrowser 表格 加载 Windows      更新时间:2023-09-26

我有一个在C#项目中生成的HTML页面。当我在IE中打开这个页面时,它就起作用了。

<!DOCTYPE HTML>
<meta http-equiv='X-UA-Compatible' content='IE=10' />
<html lang='en'>
<head>
    <title>TemplateSVG</title>
    <script type='text/javascript' src='InterfaceSVG.js'></script>
</head>
<body style='margin: 0; overflow: hidden;'>
    <div class="page-content">
        <object id='idSVG'  type='image/svg+xml' data='D:'Examples'ExampleFla.svg'></object>
    </div>
</body>
</html>

我加载了在Web浏览器中获取文本

    if (webBrowser.Document == null)
    {
        webBrowser.DocumentText = theHTMLtext;
    }
    else
    {
        webBrowser.Document.OpenNew(true);
        webBrowser.DocumentText = theHTMLtext;
    }

但是找不到文件InterfaceSVG.js

当我给出js文件src='D:'[Path]'InterfaceSVG.js'的完整路径时JS脚本使用getSVGDocument()在线生成异常。

var SvgDoc;
window.addEventListener('load', function () {  
    SvgDoc = document.getElementById("idSVG");
    if (SvgDoc == null) { alert("error"); return; }
    SvgDoc = SvgDoc.getSVGDocument(); // IE created Access Deny.
});

编辑:尝试插入js文件中的文本。

<script>Text from InterfaceSVG.js </scipt>    

但它在与getSVGDocument() 的线路上生成相同的异常(访问拒绝

我将结果HTML页面保存在一个带有SVG文件的文件夹中,并使用函数Navigate而不是DocumentText。现在它工作了。。。但我不想在磁盘上写任何东西。

    string path =  Path.GetDirectoryName(pathToSvgFile);
    string file = "''"+Path.GetFileNameWithoutExtension(pathToSvgFile);
    string newfile = path + file;
    File.WriteAllText(newfile, theHTMLtext);
    webBrowser.Navigate(newfile);

我发现了如何打开用户页面。

  1. 创建不带脚本的模板HTML页面

<!DOCTYPE HTML>
<meta http-equiv='X-UA-Compatible' content='IE=10' />
<html lang='en'>
<head>
    <title>Empty Page</title>
</head>
<body style='margin: 0; overflow: hidden; '>
    <div class="page-content">
        <object id='idSVG' style='height: 100%; width: 100%;  position:absolute;' type='image/svg+xml' data='{0}'></object>
    </div>
</body>
</html>

  1. 添加脚本文件并更改HTML页面的文本主体,这是您在事件webBrowser中所需要的。导航。

        static string PathToSvgFile;
        public static void OpenSVG(this WebBrowser webBrowser, string pathToSvgFile)
        {
            if (webBrowser.ReadyState == WebBrowserReadyState.Complete || webBrowser.ReadyState == WebBrowserReadyState.Uninitialized)
            {
                webBrowser.Navigate([Path to emptyPage.html]);
                PathToSvgFile = pathToSvgFile;
                webBrowser.Navigated += webBrowser_Navigated;
            } 
        }
        private static void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            var webBrowser = ((WebBrowser)sender);
            HtmlElementCollection head = webBrowser.Document.GetElementsByTagName("head");
            if (head != null)
            {
                var element = webBrowser.Document.CreateElement("script");
                element.SetAttribute("type", "text/javascript");
                var elementDom = (MSHTML.IHTMLScriptElement)element.DomElement;
                elementDom.src = [JavaScriptFile.js];
                ((HtmlElement)head[0]).AppendChild(element);
            }
            webBrowser.Document.Body.InnerHtml = String.Format(webBrowser.Document.Body.InnerHtml, PathToSvgFile);
            webBrowser.Navigated -= webBrowser_Navigated; 
        }