我正在尝试将 RTF 文件拆分为行(在我的代码中),但我并没有完全正确,主要是因为我并没有真正了解整个 RTF 格式。似乎可以通过 \par 或 \pard 或 \par\pard 或任意数量的有趣组合来分割行。
我正在寻找一段将文件拆分为任何语言的行的代码。
您可以尝试规范 (1.9.1)(请参阅Wikipedia 页面上的外部链接- 该页面还有几个指向几种编程语言的示例或模块的链接)。
这很可能会让您了解行插入“单词”,因此您可以使用一组明确定义的规则将文件拆分为行,而不是猜测它。
您是否遇到过Sean M. Burke撰写的 O'Reilly 的RTF 袖珍指南?
在第 13 页上,它说
以下是在 RTF 中放置换行符的一些经验法则:
或者您是否正在考虑将明文提取为行,并且无论明文的语言如何?
我编写了一个快速而肮脏的例程,它似乎适用于我能够扔给它的几乎所有东西。它在 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