Make WebBrowser control a linked list of styles in DocumentText

I am using a WebBrowser control and the text is displayed, but it does not use the associated css, it just displays as normal text. I fill it up like this

webBrowser1.DocumentText = some_text;

      

In some_text <link rel="stylesheet"href="PF.css">

along with the rest of the html

When I save some_text in a file and navigate to it by WebBrowser, it works fine webBrowser1.Navigate(@"C:\test.html");

and PF.css is in C: \

I put PF.css in the project folder where all the class files are.

How can I get the WebBrowser control to use / show the associated css file? I don't want to save my line to a file and then jump to it.

thank

+2


source to share


3 answers


mshtml.HTMLDocument CurrentDocument = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
 mshtml.IHTMLStyleSheet styleSheet = CurrentDocument.createStyleSheet("", 0);
 StreamReader streamReader = new StreamReader(@"C:\PF.css");
 string text = streamReader.ReadToEnd();
 streamReader.Close();
 styleSheet.cssText = text;

      

something like how to do it, but everything I read seems to indicate that the webbrowser control cannot execute css unless you go to the file / url and include it there. BTW, you have to add a link to Microsoft.mshtml.



Perhaps future versions of this control might handle linked stylesheets ...

+1


source


You can use inline style like ..



<html>
   <head>
       <style>
           // all my style here
       </style>
   </head>
<body>
..
..
</body>
</html>

      

0


source


If by placing it in the project folder you mean your folder bin

or the folder with the file .sln

? If it's the latter, you must put it in the same folder as your executable file (eg bin/Debug/PF.css

, bin/Release/PF.css

etc.).

Alternatively you can embed it in HTML with <style type="text/css"><!-- CSS Here --></style>

and replace the comment with your CSS. If you don't want to hardcode it where you get it some_text

, you can dynamically replace it by loading PF.css

:

using (StreamReader sr = new StreamReader("PF.css"))
{
    some_text = String.Format(some_text, sr.ReadToEnd());
}

      

This example assumes it some_text

contains the following:

<style type="text/css">
    {0}
</style>

      

0


source







All Articles