|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
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
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
|
Téléchargements
Logiciels à télécharger sur le même thème :
|