How to concatenate multiple FlowDocuments together into 1 FlowDocument

▼魔方 西西 提交于 2020-01-03 04:44:06

问题


I have multiple FlowDocuments that I would like to concatenate together. The method below doesn't have a return statement. What I would like to do is to turn the TextRange back into a 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);
   }
}

回答1:


Since FlowDocuments are just basically block collections, it is possible, and much cleaner, to simply extract the collection from the source document as a list of blocks and then insert those into the target document. Make sure to extract the blocks using ToList() or else you will get an error along the lines of "object already belongs to another collection"

try this (untested):

'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)


来源:https://stackoverflow.com/questions/9508173/how-to-concatenate-multiple-flowdocuments-together-into-1-flowdocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!