回答我自己的问题:
我的案例是创建一个文档样式显示供用户更新并保存为 PDF,但我不想依赖 Office 在我们的应用程序服务器上。
因此,我通过使用完整的“doc.RTF”文档并将其作为内存流/字符串导入并应用我需要的值更新来解决这个问题。
即VB.net sn-p 示例
Using uStream = Assembly.GetExecutingAssembly.GetManifestResourceStream("Resourcefilepath.rtf")
Using mStream As system.IO.MemoeryStream = New MemoryStream()
uStream.CopyTo(mStream)
rtfstring = Encoding.UTF8.GetSTring(mStream.toArray())
'--Do the updates to the needed string as needed:
rtfstring.Replace("Value","UpdatedValue")
'--Load Property Memory String this method is returnind
RTFDataProperty = New MemoryStream(Encoding.UTF8.GetBytes(rtfstring))
End Using
End Using
然后我将该内存流作为 DataFormats.Rtf 加载到我的 XAML 富文本框。
RichTextBox1.SelectAll()
RichTextBox1.Selection.Load(ClassName.RTFDataProperty, DataFormats.Rtf)
这给了我一个用于格式化和布局该文档的模板。 (更多的是案例场景,而不是正常做法)
我还想应用一个起始选择,所以这就是我在那里所做的:
'--Get my RichTextBox Text
rtbtext As String = New TextRange(RichTextBox1.Document.contentStart, RichTextbox1.Document.ContentEnd).Text
Dim strStartSelection As String = "Comments..."
Dim startTP As TextPointer
Dim endTP As TextPointer
'--Loop through the paragraphs of the richtextbox for my needed selection starting point:
For Each para As Paragraph In RichTextBox1.Document.Blocks
Dim paraText As String = New TextRange(para.ContentStart, para.ContentEnd).Text
If paraText = "" Then
Dim pos As TextPointer = para.ContentStart
startTP = pos
endTP = startTP.GetPositionAtOffset("".Length + 3) '--my string had ... on the end so had to add for avoiding the escape of that on length
RichTextBox1.Selection.Select(startTP, endTP)
RichTextBox1.Focus()
Exit For
End If
Next
这是简单的 VB.net 代码布局,但如果您觉得有用,可以从那里简化和调整。
谢谢