Back to Devexpress

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

wpf-117226-controls-and-libraries-rich-text-editor-examples-automation-how-to-retrieve-the-list-of-document-fonts-using-the-visitor-iterator-pattern.md

latest3.0 KB
Original Source

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

  • Oct 01, 2023
  • 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:

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.

  2. In the main class, declare new Visitor class’s object.

  3. Create a DocumentIterator class object. Pass true to the Iterator constructor as the visibleTextOnly parameter to skip hidden content.

  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.

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

  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.

See Also

How to use the Document Iterator object to iterate over document elements