WebBrowser - Find height of rendered text
I've got a web browser control that 开发者_开发技巧needs to have a specific height set. If the user has a small amount of text, I want the web browser to be short. If the user types alot of text, then I need the web browser to be alot taller.
How can I find the height of the rendered text from the WebBrowser?
I saw this Getting the page height from a C# web browser control but thought there was a better way.
Update
I'm setting the text of the browser with:
webBrowser1.DocumentText = value;
After I set this value, I can check the webBrowser1.Document.Body and its null. I can also get all children of the document and none are returned.
Am I setting the correct property?
Something I've noticed with webBrowser.DocumentText is that you cannot necessarily "get" it's value immediately after setting it it.
webBrowser1.DocumentText = "Some Text";
labelHtml.Text = webBrowser1.DocumentText
This will not produce predictable results because the webbrowser control may not have finished rendering the text yet. If it is run repeatedly, and "Some Text" is a variable, you will see that "labelHtml" quickly becomes out of sync with what is in your browser control. (The more complex "Some Text" is the longer it takes to render naturally.)
If you use the WebBrowser control's webBrowser1_documentCompleted event handler to obtain your DocumentText, you should be fine.
private void setText(string htmlText)
{
webBrowser1.DocumentText = htmlText;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
label1.Text = webBrowser1.DocumentText;
}
label1 will now contain what you provide the web browser with...
As the answer for that question:
Find the BODY tag and get the OffsetRectangle.Bottom of that element. This will give you the height of the page.
I don't see the need for any better way, it seems rather easy to me.
Does webBrowser1.DocumentText return anything? Your site contains the body tag, right?
I see a reason to do it another way...
The referenced article "Getting the page height from a C# web browser control" utilizes System.Windows.Forms.HtmlElementcollection which does not exist in the compact framework API. I am searching for solution that works within .NET CF also. I would be interested in another solution for that reason.
精彩评论