2

我有多个要连接在一起的 FlowDocument。下面的方法没有返回语句。我想做的是将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);
   }
}
4

2 回答 2

4

由于 FlowDocument 基本上只是块集合,因此可以简单地从源文档中提取集合作为块列表,然后将它们插入到目标文档中。确保使用 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)
于 2012-03-01T13:57:13.650 回答
2

@TheZenker 答案的 AC# 实现(已经过测试):

public static FlowDocument MergedFlowDoc(IEnumerable<FlowDocument> fDocs)
{
    var fDoc = new FlowDocument();
    foreach (var doc in fDocs)
    {
        fDoc.Blocks.AddRange(doc.Blocks.ToList());
    }
    return fDoc;
}
于 2020-08-03T19:53:12.183 回答