C# Render HTML to get element's height

c# html html-agility-pack

Question

I need to create an HTML page from a C# code with HTML Agility Pack. What I want is that if the element I'm trying to write cannot fit into a div with a specific height, it must create a new div and close the other one (like a paging if you want). My big concern is how to know the height of my element inside C#, I assume I'm force to render it somewhere with the CSS associated, but how?

Accepted Answer

One way to do this is using an HTML renderer like WebKit.NET.

You would then be able to call the javascript in it to retrieve DOM objects properties like heights, position, etc...

Once you have set and rendered the content you want to test (tutorial here), use StringByEvaluatingJavaScriptFromString function to access javascript functions.

string height = webKitBrowser1.StringByEvaluatingJavaScriptFromString("$('#divID').height().toString()");

Note that you need to return a string value as this function does not return other types.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why