Class DocumentVisitor

DocumentVisitor class

The abstract class for iterating through subtree with root at the specified node.

public abstract class DocumentVisitor

Methods

NameDescription
virtual VisitAttachedFileEnd(AttachedFile)End to visit the AttachedFile node.
virtual VisitAttachedFileStart(AttachedFile)Start to visit the AttachedFile node.
virtual VisitDocumentEnd(Document)End to visit the Document node.
virtual VisitDocumentStart(Document)Start to visit the Document node.
virtual VisitImageEnd(Image)End to visit the Image node.
virtual VisitImageStart(Image)Start to visit the Image node.
virtual VisitInkDrawingEnd(InkDrawing)End to visit the InkDrawing node.
virtual VisitInkDrawingStart(InkDrawing)Start to visit the InkDrawing node.
virtual VisitInkParagraphEnd(InkParagraph)End to visit the InkParagraph node.
virtual VisitInkParagraphStart(InkParagraph)Start to visit the InkParagraph node.
virtual VisitInkWordEnd(InkWord)End to visit the InkWord node.
virtual VisitInkWordStart(InkWord)Start to visit the InkWord node.
virtual VisitOutlineElementEnd(OutlineElement)End to visit the OutlineElement node.
virtual VisitOutlineElementStart(OutlineElement)Start to visit the OutlineElement node.
virtual VisitOutlineEnd(Outline)End to visit the Outline node.
virtual VisitOutlineGroupEnd(OutlineGroup)End to visit the OutlineGroup node.
virtual VisitOutlineGroupStart(OutlineGroup)Start to visit the OutlineGroup node.
virtual VisitOutlineStart(Outline)Start to visit the Outline node.
virtual VisitPageEnd(Page)End to visit the Page node.
virtual VisitPageStart(Page)Start to visit the Page node.
virtual VisitRichTextEnd(RichText)End to visit the RichText node.
virtual VisitRichTextStart(RichText)Start to visit the RichText node.
virtual VisitTableCellEnd(TableCell)End to visit the TableCell node.
virtual VisitTableCellStart(TableCell)Start to visit the TableCell node.
virtual VisitTableEnd(Table)End to visit the Table node.
virtual VisitTableRowEnd(TableRow)End to visit the TableRow node.
virtual VisitTableRowStart(TableRow)Start to visit the TableRow node.
virtual VisitTableStart(Table)Start to visit the Table node.
virtual VisitTitleEnd(Title)End to visit the Title node.
virtual VisitTitleStart(Title)Start to visit the Title node.

Examples

Shows how to access content of a document using visitor.

public static void Run()
{
    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_LoadingAndSaving();

    // Open the document we want to convert.
    Document doc = new Document(dataDir + "Aspose.one");

    // Create an object that inherits from the DocumentVisitor class.
    MyOneNoteToTxtWriter myConverter = new MyOneNoteToTxtWriter();

    // This is the well known Visitor pattern. Get the model to accept a visitor.
    // The model will iterate through itself by calling the corresponding methods
    // on the visitor object (this is called visiting).
    //
    // Note that every node in the object model has the Accept method so the visiting
    // can be executed not only for the whole document, but for any node in the document.
    doc.Accept(myConverter);

    // Once the visiting is complete, we can retrieve the result of the operation,
    // that in this example, has accumulated in the visitor.
    Console.WriteLine(myConverter.GetText());
    Console.WriteLine(myConverter.NodeCount);            
}

/// <summary>
/// Simple implementation of saving a document in the plain text format. Implemented as a Visitor.
/// </summary>
public class MyOneNoteToTxtWriter : DocumentVisitor
{
    public MyOneNoteToTxtWriter()
    {
        nodecount = 0;
        mIsSkipText = false;
        mBuilder = new StringBuilder();
    }

    /// <summary>
    /// Gets the plain text of the document that was accumulated by the visitor.
    /// </summary>
    public string GetText()
    {
        return mBuilder.ToString();
    }

    /// <summary>
    /// Adds text to the current output. Honors the enabled/disabled output flag.
    /// </summary>
    private void AppendText(string text)
    {
        if (!mIsSkipText)
        {
            mBuilder.AppendLine(text);
        }
    }

    /// <summary>
    /// Called when a RichText node is encountered in the document.
    /// </summary>
    public override void VisitRichTextStart(RichText run)
    {
        ++nodecount;
        AppendText(run.Text);
    }

    /// <summary>
    /// Called when a Document node is encountered in the document.
    /// </summary>
    public override void VisitDocumentStart(Document document)
    {
        ++nodecount;
    }

    /// <summary>
    /// Called when a Page node is encountered in the document.
    /// </summary>
    public override void VisitPageStart(Page page)
    {
        ++nodecount;
        this.AppendText($"*** Page '{page.Title?.TitleText?.Text ?? "(no title)"}' ***");
    }

    /// <summary>
    /// Called when processing of a Page node is finished.
    /// </summary>
    public override void VisitPageEnd(Page page)
    {
        this.AppendText(string.Empty);
    }

    /// <summary>
    /// Called when a Title node is encountered in the document.
    /// </summary>
    public override void VisitTitleStart(Title title)
    {
        ++nodecount;
    }

    /// <summary>
    /// Called when a Image node is encountered in the document.
    /// </summary>
    public override void VisitImageStart(Image image)
    {
        ++nodecount;
    }

    /// <summary>
    /// Called when a OutlineGroup node is encountered in the document.
    /// </summary>
    public override void VisitOutlineGroupStart(OutlineGroup outlineGroup)
    {
        ++nodecount;
    }

    /// <summary>
    /// Called when a Outline node is encountered in the document.
    /// </summary>
    public override void VisitOutlineStart(Outline outline)
    {
        ++nodecount;
    }

    /// <summary>
    /// Called when a OutlineElement node is encountered in the document.
    /// </summary>
    public override void VisitOutlineElementStart(OutlineElement outlineElement)
    {
        ++nodecount;
    }

    /// <summary>
    /// Gets the total count of nodes by the Visitor
    /// </summary>
    public Int32 NodeCount
    {
        get { return this.nodecount; }
    }

    private readonly StringBuilder mBuilder;
    private bool mIsSkipText;
    private Int32 nodecount;
}

See Also