我有多个FlowDocuments,我想连接在一起。 下面的方法没有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);
}
}
答案 0 :(得分:3)
由于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)
答案 1 :(得分:1)
@TheZenker答案的C#实现(已通过测试):
public static FlowDocument MergedFlowDoc(IEnumerable<FlowDocument> fDocs)
{
var fDoc = new FlowDocument();
foreach (var doc in fDocs)
{
fDoc.Blocks.AddRange(doc.Blocks.ToList());
}
return fDoc;
}