簡體   English   中英

Windows窗體Richtextbox。 如何在“ rtf”中查找粗體,下划線和其他文本格式以及如何為第三方供應商插入其他標簽

[英]Windows Form Richtextbox. How to find bold, underline, and other text formatting in 'rtf' and insert other tags for 3rd party vendor

謝謝閱讀。

我將RTF文本存儲在數據庫列中,該列可以包含Windows窗體Richtextbox中的粗體,斜體,下划線和其他文本格式。 我們需要將文本導出到第三方供應商,該供應商具有一組定義不同的標簽,這些標簽告訴供應商如何設置文本格式。 不幸的是,第三方供應商無法讀取RTF格式,也無法讀取HTML,XML等。

我們需要在richtextbox中導出“純文本”,但是要找到用戶在文本中添加了粗體,斜體,下划線和其他格式的位置,並替換為第三方供應商標簽。

例如...我需要解析以下rtf之類的內容...

{\\ rtf1 \\ ansi \\ ansicpg1252 \\ deff0 \\ deflang1033 {\\ fonttbl {\\ f0 \\ fnil \\ fcharset0 Verdana;}} \\ viewkind4 \\ uc1 \\ pard \\ f0 \\ fs18這是\\ b粗體\\ par \\ par \\ b0這是\\ b \\ b0 \\ i斜體字\\ par \\ par \\ i0這是\\ ul下划線\\ ulnone \\ par}

並創建如下所示的內容:

“這是[B]粗體[B]

這是[I]斜體字[I]

這是[U]下划線[U]”

有誰知道如何做到這一點?

非常感謝您的幫助。

感謝您的所有評論。 盡管效率不是很高,也不是很優雅,但這卻滿足了我的需求。 再次謝謝大家。

 Public Shared Function ParseRTF(ByRef rtf As RichTextBox) As String
        Dim returnText As String = ""
        Dim isBold As Boolean = False
        Dim isUnderline As Boolean = False
        Dim isItalics As Boolean = False
        Dim isBullets As Boolean = False

        Dim isBoldBegin As Boolean = False
        Dim isUnderlineBegin As Boolean = False
        Dim isItalicsBegin As Boolean = False
        Dim isBulletsBegin As Boolean = False

        Dim isBoldEnd As Boolean = False
        Dim isUnderlineEnd As Boolean = False
        Dim isItalicsEnd As Boolean = False
        Dim isBulletsEnd As Boolean = False

        Dim tags As String = String.Empty

        Dim bold As String = "<B>"
        Dim underline As String = "<U>"
        Dim italics As String = "<I>"
        Dim bullet As String = "<T2><BULLET>"
        Dim newLine As String = "<NL>"
        Dim lastChar As String = String.Empty

        rtf.SelectionStart = 0
        rtf.SelectionLength = 0

        Try

            For x As Integer = 0 To rtf.TextLength - 1
                rtf.SelectionStart = x
                rtf.SelectionLength = 1
                tags = String.Empty
                isBoldBegin = False
                isBoldEnd = False
                isUnderlineBegin = False
                isUnderlineEnd = False
                isItalicsBegin = False
                isItalicsEnd = False

                'Test if text has formatting
                If rtf.SelectionFont.Bold Then
                    If Not isBold Then
                        isBoldBegin = True
                        isBold = True
                        isBoldEnd = False
                    End If
                Else
                    If isBold Then
                        isBoldBegin = False
                        isBold = False
                        isBoldEnd = True
                    End If
                End If

                If rtf.SelectionFont.Italic Then
                    If Not isItalics Then
                        isItalicsBegin = True
                        isItalics = True
                        isItalicsEnd = False
                    End If
                Else
                    If isItalics Then
                        isItalicsBegin = False
                        isItalics = False
                        isItalicsEnd = True
                    End If
                End If

                If rtf.SelectionFont.Underline Then
                    If Not isUnderline Then
                        isUnderlineBegin = True
                        isUnderline = True
                        isUnderlineEnd = False
                    End If
                Else
                    If isUnderline Then
                        isUnderlineBegin = False
                        isUnderline = False
                        isUnderlineEnd = True
                    End If
                End If
                'END Test if text has formatting


                ' Do reverse order if for any "END" tags
                If isUnderlineEnd Then
                    tags += underline
                End If

                If isItalicsEnd Then
                    tags += italics
                End If

                If isBoldEnd Then
                    tags += bold
                End If

                'Work with beginning tags
                If isBoldBegin Then
                    tags += bold
                End If

                If isItalicsBegin Then
                    tags += italics
                End If

                If isUnderlineBegin Then
                    tags += underline
                End If

                If x = rtf.TextLength - 1 Then
                    If isUnderline Then
                        lastChar += underline
                    End If
                    If isItalics Then
                        lastChar += italics
                    End If
                    If isBold Then
                        lastChar += bold
                    End If
                End If

                If rtf.SelectedText.Contains(vbLf) Then
                    If Not String.IsNullOrWhiteSpace(tags) Then
                        If isBoldBegin Or isUnderlineBegin Or isItalicsBegin Then
                            returnText += newLine & tags
                        End If

                        If isBoldEnd Or isUnderlineEnd Or isItalicsEnd Then
                            returnText += tags & newLine
                        End If
                    Else
                        returnText += newLine
                    End If
                Else
                    returnText += tags & rtf.SelectedText
                End If

                If Not String.IsNullOrWhiteSpace(lastChar) Then
                    returnText += lastChar
                End If
            Next

            Return returnText


        Catch ex As Exception
            Throw New CvrException(ex, ErrorCodes.UNKNOWN_ERROR)
        Finally
        End Try
    End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM