begin process at 2012 05 25 11:44:35
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

VB.NET

 > RTF TO XHTML (BALISES P, H3, STRONG, EM)

RTF TO XHTML (BALISES P, H3, STRONG, EM)


 Information sur la source

 Description

Voilà j'ai eu besoin de faire ça alors voici le code.
Je ne gère que les caractères paragraphe, gras, italic et un style qui me sert de visuel pour une balise h3.
Je n'avais besoinde rien d'autre dans mon projet.
Pas de fichier source car cela vient d'un projet global.

Source

  • ''' <summary>
  • ''' Met en gras le texte sélectionné du richtextbox (chp_contenu1)
  • ''' </summary>
  • Private Sub BoutonGras_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BoutonGras.Click
  • Dim newFontStyle As System.Drawing.FontStyle
  • Dim currentFont As System.Drawing.Font = chp_contenu1.SelectionFont
  • If chp_contenu1.SelectionFont.Bold = True Then
  • newFontStyle = FontStyle.Regular
  • Else
  • newFontStyle = FontStyle.Bold
  • End If
  • chp_contenu1.SelectionColor = Color.Black
  • chp_contenu1.SelectionFont = New Font(currentFont.FontFamily, 10, newFontStyle)
  • End Sub
  • ''' <summary>
  • ''' Met en italic le texte sélectionné du richtextbox (chp_contenu1)
  • ''' </summary>
  • Private Sub Bouton_Italic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bouton_Italic.Click
  • Dim newFontStyle As System.Drawing.FontStyle
  • Dim currentFont As System.Drawing.Font = chp_contenu1.SelectionFont
  • If chp_contenu1.SelectionFont.Italic = True Then
  • newFontStyle = FontStyle.Regular
  • Else
  • newFontStyle = FontStyle.Italic
  • End If
  • chp_contenu1.SelectionColor = Color.Black
  • chp_contenu1.SelectionFont = New Font(currentFont.FontFamily, 10, newFontStyle)
  • End Sub
  • ''' <summary>
  • ''' style rtf qui représente ma balise h3
  • ''' </summary>
  • Private Sub Bouton_titre3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bouton_titre3.Click
  • Dim currentFont As System.Drawing.Font = chp_contenu1.SelectionFont
  • Dim TailleH3 As Integer = 10
  • Dim IndexSelection As Integer = chp_contenu1.SelectionStart
  • Dim TailleSelection As Integer = chp_contenu1.SelectionLength
  • 'copy richtextbox pour recuperer la mise en forme du caractère suivant
  • Dim Gest_RichTB As New RichTextBox
  • Gest_RichTB.Rtf = chp_contenu1.Rtf
  • Dim charSuivant As Char
  • Gest_RichTB.SelectionStart = IndexSelection + TailleSelection
  • Gest_RichTB.SelectionLength = 1
  • charSuivant = Gest_RichTB.SelectedText
  • If chp_contenu1.SelectionFont.Size = 10 Then
  • TailleH3 = 14
  • chp_contenu1.SelectionColor = Color.BlueViolet
  • Else
  • chp_contenu1.SelectionColor = Color.Black
  • End If
  • chp_contenu1.SelectionFont = New Font(currentFont.FontFamily, TailleH3, FontStyle.Regular)
  • 'retour à la ligne après
  • If charSuivant <> Chr(10) Then chp_contenu1.SelectedText += vbCrLf
  • 'Si pas premier caractère
  • If IndexSelection > 0 Then
  • chp_contenu1.SelectionStart = IndexSelection - 1
  • chp_contenu1.SelectionLength = 1
  • 'Retour à la ligne avant
  • chp_contenu1.SelectedText = chp_contenu1.SelectedText.Replace(Chr(10), "") & vbCrLf
  • End If
  • End Sub
  • ''' <summary>
  • ''' recupere le contenu de la rictextbox (contenu1) et traduction en xhtml
  • ''' </summary>
  • Private Function RTF_To_XHTML() As String
  • Dim Str_XHTML As String = ""
  • Dim IsCaractereBold As Boolean = False
  • Dim IsCaractereH3 As Boolean = False
  • Dim IsCaractereItalic As Boolean = False
  • Dim IsSuivanth3 As Boolean = False
  • Dim NextPara_close As Boolean = False 'un paragraphe est il ouvert
  • Dim TaillerichBox As Integer = chp_contenu1.Text.Length 'Nb de caractères à traiter
  • Dim CharSelected As String = "" 'caratères sélectionnés
  • Dim CharPrecedent As Char = "" 'caractère précédent
  • chp_contenu1.DeselectAll() 'aucune sélection dans la richtextbox
  • 'Traitement du premier caractère ******************************************************
  • 'selection du caractère Premier
  • chp_contenu1.SelectionStart = 0
  • chp_contenu1.SelectionLength = 1
  • 'Recup Premier caractère
  • CharSelected = chp_contenu1.SelectedText
  • 'si pas titre alors ouverture paragraphe sinon traitement
  • If (chp_contenu1.SelectionColor <> Color.BlueViolet) Then
  • Str_XHTML += "<p>"
  • NextPara_close = True
  • Else
  • Str_XHTML += "<h3>"
  • IsCaractereH3 = True
  • End If
  • If chp_contenu1.SelectionFont.Bold = True Then
  • Str_XHTML += "<strong>"
  • IsCaractereBold = True
  • ElseIf chp_contenu1.SelectionFont.Italic = True Then
  • Str_XHTML += "<em>"
  • IsCaractereItalic = True
  • End If
  • CharPrecedent = CharSelected
  • Str_XHTML += CharSelected
  • 'Traitement des autres caractères
  • For x As Integer = 1 To TaillerichBox + 1
  • 'copy richtextbox pour suivant
  • Dim Gest_RichTB As New RichTextBox
  • Gest_RichTB.Rtf = chp_contenu1.Rtf
  • Dim charSuivant As Char
  • 'selection du caractère x
  • chp_contenu1.SelectionStart = x
  • chp_contenu1.SelectionLength = 1
  • CharSelected = chp_contenu1.SelectedText
  • 'caractère suivant
  • Gest_RichTB.SelectionStart = x + 1
  • Gest_RichTB.SelectionLength = 1
  • charSuivant = Gest_RichTB.SelectedText
  • If CharSelected = Chr(10) Then
  • If Gest_RichTB.SelectionColor = Color.BlueViolet Then
  • IsSuivanth3 = True
  • End If
  • If NextPara_close Then
  • Str_XHTML += "</p>" & vbCrLf
  • NextPara_close = False
  • If Not IsSuivanth3 Then
  • Str_XHTML += vbCrLf & "<p>"
  • NextPara_close = True
  • IsSuivanth3 = False
  • End If
  • Else
  • If IsCaractereH3 Then
  • Str_XHTML += "</h3>"
  • IsCaractereH3 = False
  • End If
  • If Gest_RichTB.SelectionColor <> Color.BlueViolet Then
  • Str_XHTML += vbCrLf & "<p>"
  • NextPara_close = True
  • End If
  • End If
  • Else
  • If chp_contenu1.SelectionColor = Color.BlueViolet And Not IsCaractereH3 Then
  • Str_XHTML += "<h3>" & CharSelected
  • IsCaractereH3 = True
  • ElseIf chp_contenu1.SelectionFont.Bold And Not IsCaractereBold Then
  • Str_XHTML += "<strong>" & CharSelected
  • IsCaractereBold = True
  • ElseIf Not Gest_RichTB.SelectionFont.Bold And IsCaractereBold Then
  • Str_XHTML += CharSelected & "</strong>"
  • IsCaractereBold = False
  • ElseIf chp_contenu1.SelectionFont.Italic And Not IsCaractereItalic Then
  • Str_XHTML += "<em>" & CharSelected
  • IsCaractereItalic = True
  • ElseIf Not Gest_RichTB.SelectionFont.Italic And IsCaractereItalic Then
  • Str_XHTML += CharSelected & "</em>"
  • IsCaractereItalic = False
  • Else
  • Str_XHTML += CharSelected
  • End If
  • End If
  • Next
  • If IsCaractereBold Then Str_XHTML += "</strong>"
  • If IsCaractereItalic Then Str_XHTML += "</em>"
  • If IsCaractereH3 Then Str_XHTML += "</h3>"
  • If NextPara_close Then Str_XHTML += "</p>"
  • Return Str_XHTML.Replace("<p></p>", "")
  • End Function
 ''' <summary>
    ''' Met en gras le texte sélectionné du richtextbox (chp_contenu1)
    ''' </summary>
    Private Sub BoutonGras_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BoutonGras.Click
        Dim newFontStyle As System.Drawing.FontStyle
        Dim currentFont As System.Drawing.Font = chp_contenu1.SelectionFont

        If chp_contenu1.SelectionFont.Bold = True Then
            newFontStyle = FontStyle.Regular
        Else
            newFontStyle = FontStyle.Bold
        End If
        chp_contenu1.SelectionColor = Color.Black
        chp_contenu1.SelectionFont = New Font(currentFont.FontFamily, 10, newFontStyle)
    End Sub

    ''' <summary>
    ''' Met en italic le texte sélectionné du richtextbox (chp_contenu1)
    ''' </summary>
    Private Sub Bouton_Italic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bouton_Italic.Click
        Dim newFontStyle As System.Drawing.FontStyle
        Dim currentFont As System.Drawing.Font = chp_contenu1.SelectionFont

        If chp_contenu1.SelectionFont.Italic = True Then
            newFontStyle = FontStyle.Regular
        Else
            newFontStyle = FontStyle.Italic
        End If
        chp_contenu1.SelectionColor = Color.Black
        chp_contenu1.SelectionFont = New Font(currentFont.FontFamily, 10, newFontStyle)
    End Sub

    ''' <summary>
    ''' style rtf qui représente ma balise h3
    ''' </summary>
    Private Sub Bouton_titre3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bouton_titre3.Click

        Dim currentFont As System.Drawing.Font = chp_contenu1.SelectionFont
        Dim TailleH3 As Integer = 10
        Dim IndexSelection As Integer = chp_contenu1.SelectionStart
        Dim TailleSelection As Integer = chp_contenu1.SelectionLength

        'copy richtextbox pour recuperer la mise en forme du caractère suivant
        Dim Gest_RichTB As New RichTextBox
        Gest_RichTB.Rtf = chp_contenu1.Rtf
        Dim charSuivant As Char
        Gest_RichTB.SelectionStart = IndexSelection + TailleSelection
        Gest_RichTB.SelectionLength = 1
        charSuivant = Gest_RichTB.SelectedText


        If chp_contenu1.SelectionFont.Size = 10 Then
            TailleH3 = 14
            chp_contenu1.SelectionColor = Color.BlueViolet
        Else
            chp_contenu1.SelectionColor = Color.Black
        End If
        chp_contenu1.SelectionFont = New Font(currentFont.FontFamily, TailleH3, FontStyle.Regular)
        'retour à la ligne après
        If charSuivant <> Chr(10) Then chp_contenu1.SelectedText += vbCrLf
        'Si pas premier caractère
        If IndexSelection > 0 Then
            chp_contenu1.SelectionStart = IndexSelection - 1
            chp_contenu1.SelectionLength = 1
            'Retour à la ligne avant
            chp_contenu1.SelectedText = chp_contenu1.SelectedText.Replace(Chr(10), "") & vbCrLf
        End If
    End Sub

    ''' <summary>
    ''' recupere le contenu de la rictextbox (contenu1) et traduction en xhtml
    ''' </summary>
    Private Function RTF_To_XHTML() As String
        Dim Str_XHTML As String = ""

        Dim IsCaractereBold As Boolean = False
        Dim IsCaractereH3 As Boolean = False
        Dim IsCaractereItalic As Boolean = False
        Dim IsSuivanth3 As Boolean = False


        Dim NextPara_close As Boolean = False 'un paragraphe est il ouvert

        Dim TaillerichBox As Integer = chp_contenu1.Text.Length 'Nb de caractères à traiter
        Dim CharSelected As String = "" 'caratères sélectionnés
        Dim CharPrecedent As Char = "" 'caractère précédent


        chp_contenu1.DeselectAll() 'aucune sélection dans la richtextbox

        'Traitement du premier caractère ******************************************************
        'selection du caractère Premier
        chp_contenu1.SelectionStart = 0
        chp_contenu1.SelectionLength = 1
        'Recup Premier caractère
        CharSelected = chp_contenu1.SelectedText
        'si pas titre alors ouverture paragraphe sinon traitement
        If (chp_contenu1.SelectionColor <> Color.BlueViolet) Then
            Str_XHTML += "<p>"
            NextPara_close = True
        Else
            Str_XHTML += "<h3>"
            IsCaractereH3 = True
        End If
        If chp_contenu1.SelectionFont.Bold = True Then
            Str_XHTML += "<strong>"
            IsCaractereBold = True
        ElseIf chp_contenu1.SelectionFont.Italic = True Then
            Str_XHTML += "<em>"
            IsCaractereItalic = True

        End If
        CharPrecedent = CharSelected
        Str_XHTML += CharSelected

        'Traitement des autres caractères
        For x As Integer = 1 To TaillerichBox + 1
            'copy richtextbox pour suivant
            Dim Gest_RichTB As New RichTextBox
            Gest_RichTB.Rtf = chp_contenu1.Rtf
            Dim charSuivant As Char

            'selection du caractère x
            chp_contenu1.SelectionStart = x
            chp_contenu1.SelectionLength = 1
            CharSelected = chp_contenu1.SelectedText


            'caractère suivant
            Gest_RichTB.SelectionStart = x + 1
            Gest_RichTB.SelectionLength = 1
            charSuivant = Gest_RichTB.SelectedText


            If CharSelected = Chr(10) Then
                If Gest_RichTB.SelectionColor = Color.BlueViolet Then
                    IsSuivanth3 = True
                End If
                If NextPara_close Then

                    Str_XHTML += "</p>" & vbCrLf
                    NextPara_close = False
                    If Not IsSuivanth3 Then
                        Str_XHTML += vbCrLf & "<p>"
                        NextPara_close = True
                        IsSuivanth3 = False
                    End If
                Else
                    If IsCaractereH3 Then
                        Str_XHTML += "</h3>"
                        IsCaractereH3 = False
                    End If
                    If Gest_RichTB.SelectionColor <> Color.BlueViolet Then
                        Str_XHTML += vbCrLf & "<p>"
                        NextPara_close = True
                    End If

                End If
            Else
                If chp_contenu1.SelectionColor = Color.BlueViolet And Not IsCaractereH3 Then
                    Str_XHTML += "<h3>" & CharSelected
                    IsCaractereH3 = True
                ElseIf chp_contenu1.SelectionFont.Bold And Not IsCaractereBold Then
                    Str_XHTML += "<strong>" & CharSelected
                    IsCaractereBold = True
                ElseIf Not Gest_RichTB.SelectionFont.Bold And IsCaractereBold Then
                    Str_XHTML += CharSelected & "</strong>"
                    IsCaractereBold = False
                ElseIf chp_contenu1.SelectionFont.Italic And Not IsCaractereItalic Then
                    Str_XHTML += "<em>" & CharSelected
                    IsCaractereItalic = True
                ElseIf Not Gest_RichTB.SelectionFont.Italic And IsCaractereItalic Then
                    Str_XHTML += CharSelected & "</em>"
                    IsCaractereItalic = False
                Else
                    Str_XHTML += CharSelected
                End If
            End If


        Next
        If IsCaractereBold Then Str_XHTML += "</strong>"
        If IsCaractereItalic Then Str_XHTML += "</em>"
        If IsCaractereH3 Then Str_XHTML += "</h3>"
        If NextPara_close Then Str_XHTML += "</p>"

        Return Str_XHTML.Replace("<p></p>", "")
    End Function

 Conclusion

J'utilise le caractère suivant afin de bien fermer mes balises


 Sources du même auteur

Source .NET (Dotnet) CLASSE POUR L'UTILISATION D'EXPRESSIONS RÉGULIÈRES
Source avec Zip Source .NET (Dotnet) XML VALIDATION PAR SCHÉMA (XSD)
Source .NET (Dotnet) DOTNET 2.0 TRANSFORMATION XSLT D'UN FICHIER XML (VB.NET)
Source avec une capture SERVERXMLHTTP : AUTOMATISATION D'ENVOI D'UN FORMULAIRE AVEC...

 Sources de la même categorie

Source .NET (Dotnet) UTILISATION DE L'API SENDMESSAGE ENTRE DEUX APPLICATIONS VB.... par chris_brabant
Source avec Zip APPLICATION MULTILANGUES (LOCALISATION) par 310
Source avec Zip Source .NET (Dotnet) CALCULER LA TAILLE D'UN DOSSIER ET SER SOU_RÉPÉRTOIRES VERSI... par 310
Source avec Zip GESTION DES PHOTOS EN UTILISANT UNE BASE ACCESS par 310
Source .NET (Dotnet) CALCULER LA TAILLE D'UN DOSSIER ET SER SOU_RÉPÉRTOIRES par 310

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture CONVERTIR DU TEXTE RTF EN CODE HTML ET VICE-VERSA par vicosta
Source avec Zip Source .NET (Dotnet) RTFANSITEXTWRITER : GÉNÉRER DU RTF EN VB.NET ET CONVERTIR DU... par ShareVB
CONVERTISSEUR RTF TO HTML par tontonkika
Source avec Zip Source avec une capture CONVERTIR LE CONTENU D'UN RICHTEXTBOX EN HTML par Gael51
RTF --> CODE HTML par Caïn

Commentaires et avis

Commentaire de saizonou le 12/01/2007 16:13:58

Je me sers du code généré dans une base afin que mes users puissent saisir des bouts de html (sans le connaître) récupérés et mis en forme ensuite en css.

Commentaire de wanderz le 07/02/2007 20:45:42

Ton code ne me concerti que la première lettre de la chaine de caractères :(

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Convertion HTML en Gif ou jpg ? SVP [ par nounours22 ] Bonjour,quelqu'un pourrait il me dire comment effectuer une vconvertion d'une page HTML en Image par porgrammation ?Merci de vos réponses à venir.noun SVP Help me >Conversion rtf html [ par pekinio ] hello a tlmvoila, en fait, je voudrais enregistrer le contenu d'une rtf box , avec caracteres en couleurs, eventuellement des photos et tout, en html. Imprimer un fichier (html. rtf..) en VB? [ par jeromax ] Tout est dit dans le sujetMerci Bilan => Conversion Rtf to HTML ! Propriétés Emplacement [ par scottmat ] <span style="FONT-SIZE: 12pt; COLOR: #4f81bd; LINE-HEIGHT: 115%; FONT-FAMILY: 'Verdana','sans-serif'; mso-themec Conversion RTF/TXT -> HTML [ par OneHacker ] Je programme avec VS Express 2005 .NET. Je voulais savoir s'il était possible de convertir du texte simple HTML ou du RTF en HTML.J'ai essayé ça mais Rtf to Html via Word automation [ par pattex62 ] Rtf to Html via Word automation  Bonjour à tous !!! Je souhaiterais convertir le contenu d'un Richtexbox (format RTF) en format HTML !!! </p Rtf Richtextbox vers html [ par amita ] salut les amis surment que j'ai trové plein de source concernant cette question mais ce que j'arrive pas a trouver c'est comment convertir une image i Convertir rtf/text richtextbox en Html, Binaire, Héxadécimale et Décimale VB.NET [ par Gabilach ] Bonjour, cela fait longtemps que je cherche et je ne trouve vraiment rien qui réponde vraiment à mes attentes, je voudrait convertir du text/rtf dans Lire contenu Page HTML, rechercher informations, et afficher label ... [ par onsedemande ] Merci, merci d’avance pour votre aide, je poste à présent dans la bonne section. Etant novice sous viual basic express 10, je cherche à récupérer des


Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 2,106 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales