Convertir le contenu d'une richtextbox en html (vb.net)

Contenu du snippet

Voila une petite fonction qui converti le contenu d'une richtextbox en code HTML

Source / Exemple :


Private Function ConvertToHTML(ByVal Box As RichTextBox) As String
        Dim strHTML As String
        Dim strColeur As String
        Dim blnGras As Boolean
        Dim blnItalic As Boolean
        Dim strPolice As String
        Dim shtTaille As Short
        Dim lngDepartOriginal As Long
        Dim lngTailleOriginal As Long
        Dim intCount As Integer

        ' Si le text est vide, on sort
        If Box.Text.Length = 0 Then Exit Function

        ' Conserver la selection originale, et selectionné le debut
        lngDepartOriginal = 0
        lngTailleOriginal = Box.TextLength
        Box.Select(0, 1)

        ' Entete HTML
        strHTML = "<HTML>"

        ' Récuperer les parametres initaux
        strColeur = Box.SelectionColor.ToKnownColor.ToString
        blnGras = Box.SelectionFont.Bold
        blnItalic = Box.SelectionFont.Italic
        strPolice = Box.SelectionFont.FontFamily.Name
        shtTaille = Box.SelectionFont.Size
        ' Inclure le premier parametre HTML "Style"
        strHTML += "<SPAN style=""font-family: " & strPolice & _
          "; font-size: " & shtTaille & "pt; color: " _
                          & strColeur & """>"
        ' Inclure le TAg GRAS si besoin est
        If blnGras = True Then
            strHTML += "<B>"
        End If
        ' Inclure le TAg ITALIQUE si besoin est
        If blnItalic = True Then
            strHTML += "<I>"
        End If

        ' Finalement on attaque le premier caractère
        strHTML += Box.Text.Substring(0, 1)

        ' Boucle sur le reste du texte
        For intCount = 2 To Box.Text.Length

            ' Selection du caractere
            Box.Select(intCount - 1, 1)

            ' Verifier et implementer si necessaire un changement de style
            If Box.SelectionColor.ToKnownColor.ToString <> strColeur _
                 Or Box.SelectionFont.FontFamily.Name <> strPolice _
                 Or Box.SelectionFont.Size <> shtTaille Then
                strHTML += "</SPAN><SPAN style=""font-family: " _
                  & Box.SelectionFont.FontFamily.Name & _
                  "; font-size: " & Box.SelectionFont.Size & _
                  "pt; color: " & _
                  Box.SelectionColor.ToKnownColor.ToString & """>"
            End If
            ' Verifier changement GRAS
            If Box.SelectionFont.Bold <> blnGras Then
                If Box.SelectionFont.Bold = False Then
                    strHTML += "</B>"
                Else
                    strHTML += "<B>"
                End If
            End If
            ' Verifier changement ITALIQUE
            If Box.SelectionFont.Italic <> blnItalic Then
                If Box.SelectionFont.Italic = False Then
                    strHTML += "</I>"
                Else
                    strHTML += "<I>"
                End If
            End If

            ' Ajouter le caractere
            Select Case Box.Text.Substring(intCount - 1, 1)
                Case ControlChars.Lf ' Si c'est un LineFeed, mettre la borne <BR>
                    strHTML += "<BR>"
                Case " "
                    strHTML += "&nbsp;"
                Case ControlChars.Tab
                    strHTML += "&nbsp;&nbsp;&nbsp;&nbsp;"
                Case Else
                    strHTML += System.Web.HttpUtility.HtmlEncode(Box.Text.Substring(intCount - 1, 1))
            End Select

            ' Mise a jour du style courant
            strColeur = Box.SelectionColor.ToKnownColor.ToString
            blnGras = Box.SelectionFont.Bold
            blnItalic = Box.SelectionFont.Italic
            strPolice = Box.SelectionFont.FontFamily.Name
            shtTaille = Box.SelectionFont.Size
        Next

        ' Fermer les Tag <B> <I> si necessaire
        If blnGras = True Then strHTML += "</B>"
        If blnItalic = True Then strHTML += "</I>"

        ' Fermer le style et la page HTML
        strHTML += "</SPAN></HTML>"

        ' Restorer la selection d'origine
        Box.Select(lngDepartOriginal, lngTailleOriginal)

        ' Retourner le code HTML
        Return strHTML
    End Function

Conclusion :


voila et aprés :

Chaine_HTML = ConvertToHTML (Controle_RichText)

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.