如何将RTF文件拆分成行?

时间:2009-08-21 04:07:11

标签: vb6 rtf

我正在尝试将RTF文件拆分为行(在我的代码中)并且我不是很正确,主要是因为我并不是真正想要整个RTF格式。似乎可以通过\ par或\ pard或\ par \ pard或任意数量的有趣组合来分割线条。

我正在寻找一段代码,可以将文件分成任何语言的行。

3 个答案:

答案 0 :(得分:1)

您可以尝试the specification (1.9.1)(请参阅维基百科页面上的External Links - 其中包含几个编程语言示例或模块的链接)。

这很可能会让您了解行插入“单词”,因此您可以使用明确定义的规则将文件拆分为行,而不是猜测它。

答案 1 :(得分:1)

你有没有见过O'Reilly的RTF Pocket Guide,Sean M. Burke?

第13页,它说

以下是将换行符放入RTF的一些经验法则:

  • 在每个\ pard或\之前加上换行符(“段落”部分中解释的命令。
  • 在RTF字体表,样式表和其他类似结构之前和之后添加换行符(如颜色表,稍后介绍)。
  • 您可以在每个 N 空格{,或}之后添加换行符。 (或者:在第60列之后的每个空格{,或}之后添加换行符。)

或者您是在考虑将明文提取为行,并且不管明文的语言是什么?

答案 2 :(得分:1)

我编写了一个快速而肮脏的例程,它似乎适用于我能够抛出的任何东西。它在VB6中,但很容易翻译成其他任何东西。

Private Function ParseRTFIntoLines(ByVal strSource As String) As Collection
    Dim colReturn As Collection
    Dim lngPosStart As Long
    Dim strLine As String
    Dim sSplitters(1 To 4) As String
    Dim nIndex As Long

    ' return collection of lines '

    ' The lines can be split by the following '
    ' "\par"                                  '
    ' "\par "                                 '
    ' "\par\pard "                            '

    ' Add these splitters in order so that we do not miss '
    ' any possible split combos, for instance, "\par\pard" is added before "\par" '
    ' because if we look for "\par" first, we will miss "\par\pard" '
    sSplitters(1) = "\par \pard"
    sSplitters(2) = "\par\pard"
    sSplitters(3) = "\par "
    sSplitters(4) = "\par"

    Set colReturn = New Collection

    ' We have to find each variation '
    ' We will look for \par and then evaluate which type of separator is there '

    Do
        lngPosStart = InStr(1, strSource, "\par", vbTextCompare)
        If lngPosStart > 0 Then
            strLine = Left$(strSource, lngPosStart - 1)

            For nIndex = 1 To 4
                If StrComp(sSplitters(nIndex), Mid$(strSource, lngPosStart, Len(sSplitters(nIndex))), vbTextCompare) = 0 Then
                    ' remove the 1st line from strSource '
                    strSource = Mid$(strSource, lngPosStart + Len(sSplitters(nIndex)))

                    ' add to collection '
                    colReturn.Add strLine

                    ' get out of here '
                    Exit For
                End If
            Next
        End If

    Loop While lngPosStart > 0

    ' check to see whether there is a last line '
    If Len(strSource) > 0 Then colReturn.Add strSource

    Set ParseRTFIntoLines = colReturn
End Function