Skip to main content

How to: Retrieve the List of Document Fonts using the Visitor-Iterator Pattern

  • 3 minutes to read

You can use the DocumentIterator to retrieve all fonts used in the document and display them in the ListBoxControl, as shown below:

DocumentIterator_Example

Follow the steps below to complete this task:

  1. Declare the DocumentVisitorBase class descendant. The DocumentVisitorBase implements the IDocumentVisitor interface. This interface provides the Visit method overloads for every document element type.

    Create the Fonts property to return the list of fonts retrieved by the Visitor.

    Override the corresponding IDocumentVisitor.Visit method to retrieve the DocumentText element ‘s ReadOnlyTextPropertiesBase.FontName value and add it to the fonts’ list.

    Important

    You cannot remove the visited element’s content or add new content.

    public class MyVisitor : DocumentVisitorBase
    {
      private List<string> fonts;
      public List<string> DocumentFonts {get {return fonts;}}
      public MyVisitor()
       {
         fonts = new List<string>();
       }
    
      public override void Visit(DocumentText text) 
      {
       if (!fonts.Contains(text.TextProperties.FontName))
           fonts.Add(text.TextProperties.FontName);
       }
    }
    
  2. In the main class, declare a new Visitor class’s object.

    MyVisitor visitor = new MyVisitor();
    
  3. Create a DocumentIterator class object. Pass true to the Iterator constructor as the visibleTextOnly parameter to skip hidden content.

    DocumentIterator iterator = new DocumentIterator(richEditControl1.Document, true);
    
  4. Call the DocumentIterator.MoveNext method to process each element, and use the IDocumentElement.Accept method to allow the Visitor to access the retrieved entity (returned by the DocumentIterator.Current property). When the iterator reaches the end of the document, the IDocumentElement.Accept method returns false. As a result, the Visitor executes the related Visit method for every iterated element.

    Pass one of the DocumentElementType values as a parameter to DocumentIterator.MoveNext method to check a specific type of elements.

    while (iterator.MoveNext())
        iterator.Current.Accept(visitor);
    
  5. Pass the retrieved fonts’ list as the BaseListBoxControl.DataSource property value to display the list the ListBoxControl.

    listBoxControl1.DataSource = visitor.DocumentFonts;
    
  6. Some elements, such as DocumentTextBox or DocumentCommentElement, can contain other elements. Create a new visitor and iterator instance in the corresponding Visit method to get these entities’ content. Use DocumentTextBox.GetIterator or DocumentCommentElement.GetIterator method to provide a new iterator with access to the element’s content. Pass true as the method’s visibleTextOnly parameter to skip hidden content.

    Note

    The iterator obtains only the main body’s elements. The header’s or footer’s entities are not accessible to it.

    public override void Visit(DocumentTextBox textbox)
     {
       DocumentIterator textBoxIterator = textBox.GetIterator(true);
       MyVisitor textBoxVisitor = new MyVisitor();
         while (textBoxIterator.MoveNext())
            textBoxIterator.Current.Accept(textBoxVisitor);
     }
    
See Also