遍历 FlowDocument

我们经常要遍历FlowDocument文档 查找元素,具体的写法之一 :

打印出文档的结构

  private void TransformImagesTo642(FlowDocument flowDocument)
  {
      TextPointer navigator = flowDocument.ContentStart;
      while (navigator.CompareTo(flowDocument.ContentEnd) < 0)
      {
          switch (navigator.GetPointerContext(LogicalDirection.Forward))
          {
              case TextPointerContext.ElementStart:
                  // Output opening tag of a TextElement
               Debug.WriteLine("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
                  break;
              case TextPointerContext.ElementEnd:
                  // Output closing tag of a TextElement
                  Debug.WriteLine("</{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
                  break;
              case TextPointerContext.EmbeddedElement:
                  // Output simple tag for embedded element
                  Debug.WriteLine("<{0}/>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
                  break;
              case TextPointerContext.Text:
                  // Output the text content of this text run
                  Debug.WriteLine(navigator.GetTextInRun(LogicalDirection.Forward));
                  break;
          }

          // Advance the naviagtor to the next context position.
          navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
      } // End while.

  }

 

posted @ 2024-04-26 06:37  小林野夫  阅读(34)  评论(0)    收藏  举报
原文链接:https://www.cnblogs.com/cdaniu/