Skip to main content

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

  • 3 minutes to read

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

FontsList

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 FontName value and add it to the fonts’ list.

    Important

    You cannot remove the visited element’s content or add a 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 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 IDocumentElement.Accept method returns returns false, the iterator has reached the end of the document. As a result, the Visitor executes the related Visit method for every iterated element.

    while (iterator.MoveNext())
        iterator.Current.Accept(visitor);
    

    Tip

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

  5. Pass the retrieved fonts’ list as the BaseListBoxEdit.ItemsSource property value to display the list the ListBoxEdit.

    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 the 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