locked
Can I get font size of htmlelement?

    Question

  • Hi

    Can I get font size of html element in any html document? If yes, How.

    thanks

    Saturday, December 20, 2008 10:31 AM

Answers

  • The answer is essentially yes (the browser must be able to get the font in order to display it).

    So the information is in the html document... the question is where.

    To ready any font from any html element, you'll need to write a fairly sophisticated algorithm which more-or-less does the following:

    Examine the target element for a Font attribute.
    If not, check for Font in Style
    If not, check for Font in parent element.
    If not, check for Font in Font tag
    If not...

    You get the picture.  You'll have to follow all the possible rules for applying a font in HTML.  You'll also have to take Style sheets into account.  And when you find more than one match, you'll have to apply HTML precedence rules to decide which Font properties apply (you might actually have to examine several tags in order to determine all of the font properties for a specific element).

    This certainly isn't impossible, but neither is it for the faint of heart.
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Wednesday, December 24, 2008 12:45 AM
  • Try this code, use it when browser's documentcompleted fires, this might not be complete code for your requirement but should give you starting point.

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.ObjectByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 
            Dim elements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Font"
            For Each h As HtmlElement In elements 
                If Not h.InnerText Is Nothing Then 
                    MessageBox.Show("Font Size:" & h.GetAttribute("size") & vbNewLine & "Text: " & h.InnerText) 
                End If 
            Next 
        End Sub 


    Arjun Paudel
    • Marked as answer by jonny999 Sunday, December 28, 2008 7:04 PM
    Saturday, December 27, 2008 12:07 PM

All replies

  • The answer is essentially yes (the browser must be able to get the font in order to display it).

    So the information is in the html document... the question is where.

    To ready any font from any html element, you'll need to write a fairly sophisticated algorithm which more-or-less does the following:

    Examine the target element for a Font attribute.
    If not, check for Font in Style
    If not, check for Font in parent element.
    If not, check for Font in Font tag
    If not...

    You get the picture.  You'll have to follow all the possible rules for applying a font in HTML.  You'll also have to take Style sheets into account.  And when you find more than one match, you'll have to apply HTML precedence rules to decide which Font properties apply (you might actually have to examine several tags in order to determine all of the font properties for a specific element).

    This certainly isn't impossible, but neither is it for the faint of heart.
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Wednesday, December 24, 2008 12:45 AM
  • It means just via Htmlelement.Style or is there any other way like GetAtribute ...?
    Can you write me simple code ,please.

    And Thanks for help 
    Saturday, December 27, 2008 11:09 AM
  • Try this code, use it when browser's documentcompleted fires, this might not be complete code for your requirement but should give you starting point.

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.ObjectByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 
            Dim elements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Font"
            For Each h As HtmlElement In elements 
                If Not h.InnerText Is Nothing Then 
                    MessageBox.Show("Font Size:" & h.GetAttribute("size") & vbNewLine & "Text: " & h.InnerText) 
                End If 
            Next 
        End Sub 


    Arjun Paudel
    • Marked as answer by jonny999 Sunday, December 28, 2008 7:04 PM
    Saturday, December 27, 2008 12:07 PM
  •  Perfect, Thank you very much.
    Sunday, December 28, 2008 7:03 PM