如何将多个 FlowDocument 连接成 1 个 FlowDocument

B4n*_*ndt 3 c# wpf flowdocument textrange

我有多个 FlowDocument,我想将它们连接在一起。下面的方法没有 return 语句。我想做的是将 TextRange 转回 FlowDocument。

private FlowDocument Concatenate(FlowDocument source, FlowDocument target)
{   using(MemoryStream ms = new MemoryStream())
    {
      TextRange tr = new TextRange(source.ContentStart, source.ContentEnd);
      tr.Save(ms, DataFormats.XamlPackage);
      ms.Seek(0, SeekOrigin.Begin);
      tr = new TextRange(target.ContentEnd, target.ContentEnd);
      tr.Load(ms, DataFormats.XamlPackage);
   }
}
Run Code Online (Sandbox Code Playgroud)

The*_*ker 5

由于 FlowDocuments 基本上只是块集合,因此可以简单地从源文档中提取集合作为块列表,然后将其插入到目标文档中,而且更加简洁。确保使用 ToList() 提取块,否则您将收到类似“对象已属于​​另一个集合”的错误

试试这个(未经测试):

'targetDocument is flowdocument that will be aggregate of both
'insertDocument contains document content you want to insert into target
 Dim insertBlocks As List(Of Block) = insertDocument.Blocks.ToList()
 targetDocument.Blocks.AddRange(insertBlocks)
Run Code Online (Sandbox Code Playgroud)