Aspose::Words::DocumentVisitor class

DocumentVisitor class

Base class for custom document visitors. To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.

class DocumentVisitor : public virtual System::Object

Methods

MethodDescription
GetType() const override
Is(const System::TypeInfo&) const override
static Type()
virtual VisitAbsolutePositionTab(System::SharedPtr<Aspose::Words::AbsolutePositionTab>)Called when a AbsolutePositionTab node is encountered in the document.
virtual VisitBodyEnd(System::SharedPtr<Aspose::Words::Body>)Called when enumeration of the main text story in a section has ended.
virtual VisitBodyStart(System::SharedPtr<Aspose::Words::Body>)Called when enumeration of the main text story in a section has started.
virtual VisitBookmarkEnd(System::SharedPtr<Aspose::Words::BookmarkEnd>)Called when an end of a bookmark is encountered in the document.
virtual VisitBookmarkStart(System::SharedPtr<Aspose::Words::BookmarkStart>)Called when a start of a bookmark is encountered in the document.
virtual VisitBuildingBlockEnd(System::SharedPtr<Aspose::Words::BuildingBlocks::BuildingBlock>)Called when enumeration of a building block has ended.
virtual VisitBuildingBlockStart(System::SharedPtr<Aspose::Words::BuildingBlocks::BuildingBlock>)Called when enumeration of a building block has started.
virtual VisitCellEnd(System::SharedPtr<Aspose::Words::Tables::Cell>)Called when enumeration of a table cell has ended.
virtual VisitCellStart(System::SharedPtr<Aspose::Words::Tables::Cell>)Called when enumeration of a table cell has started.
virtual VisitCommentEnd(System::SharedPtr<Aspose::Words::Comment>)Called when enumeration of a comment text has ended.
virtual VisitCommentRangeEnd(System::SharedPtr<Aspose::Words::CommentRangeEnd>)Called when the end of a commented range of text is encountered.
virtual VisitCommentRangeStart(System::SharedPtr<Aspose::Words::CommentRangeStart>)Called when the start of a commented range of text is encountered.
virtual VisitCommentStart(System::SharedPtr<Aspose::Words::Comment>)Called when enumeration of a comment text has started.
virtual VisitDocumentEnd(System::SharedPtr<Aspose::Words::Document>)Called when enumeration of the document has finished.
virtual VisitDocumentStart(System::SharedPtr<Aspose::Words::Document>)Called when enumeration of the document has started.
virtual VisitEditableRangeEnd(System::SharedPtr<Aspose::Words::EditableRangeEnd>)Called when an end of an editable range is encountered in the document.
virtual VisitEditableRangeStart(System::SharedPtr<Aspose::Words::EditableRangeStart>)Called when a start of an editable range is encountered in the document.
virtual VisitFieldEnd(System::SharedPtr<Aspose::Words::Fields::FieldEnd>)Called when a field ends in the document.
virtual VisitFieldSeparator(System::SharedPtr<Aspose::Words::Fields::FieldSeparator>)Called when a field separator is encountered in the document.
virtual VisitFieldStart(System::SharedPtr<Aspose::Words::Fields::FieldStart>)Called when a field starts in the document.
virtual VisitFootnoteEnd(System::SharedPtr<Aspose::Words::Notes::Footnote>)Called when enumeration of a footnote or endnote text has ended.
virtual VisitFootnoteStart(System::SharedPtr<Aspose::Words::Notes::Footnote>)Called when enumeration of a footnote or endnote text has started.
virtual VisitFormField(System::SharedPtr<Aspose::Words::Fields::FormField>)Called when a form field is encountered in the document.
virtual VisitGlossaryDocumentEnd(System::SharedPtr<Aspose::Words::BuildingBlocks::GlossaryDocument>)Called when enumeration of a glossary document has ended.
virtual VisitGlossaryDocumentStart(System::SharedPtr<Aspose::Words::BuildingBlocks::GlossaryDocument>)Called when enumeration of a glossary document has started.
virtual VisitGroupShapeEnd(System::SharedPtr<Aspose::Words::Drawing::GroupShape>)Called when enumeration of a group shape has ended.
virtual VisitGroupShapeStart(System::SharedPtr<Aspose::Words::Drawing::GroupShape>)Called when enumeration of a group shape has started.
virtual VisitHeaderFooterEnd(System::SharedPtr<Aspose::Words::HeaderFooter>)Called when enumeration of a header or footer in a section has ended.
virtual VisitHeaderFooterStart(System::SharedPtr<Aspose::Words::HeaderFooter>)Called when enumeration of a header or footer in a section has started.
virtual VisitOfficeMathEnd(System::SharedPtr<Aspose::Words::Math::OfficeMath>)Called when enumeration of a Office Math object has ended.
virtual VisitOfficeMathStart(System::SharedPtr<Aspose::Words::Math::OfficeMath>)Called when enumeration of a Office Math object has started.
virtual VisitParagraphEnd(System::SharedPtr<Aspose::Words::Paragraph>)Called when enumeration of a paragraph has ended.
virtual VisitParagraphStart(System::SharedPtr<Aspose::Words::Paragraph>)Called when enumeration of a paragraph has started.
virtual VisitRowEnd(System::SharedPtr<Aspose::Words::Tables::Row>)Called when enumeration of a table row has ended.
virtual VisitRowStart(System::SharedPtr<Aspose::Words::Tables::Row>)Called when enumeration of a table row has started.
virtual VisitRun(System::SharedPtr<Aspose::Words::Run>)Called when a run of text in the is encountered.
virtual VisitSectionEnd(System::SharedPtr<Aspose::Words::Section>)Called when enumeration of a section has ended.
virtual VisitSectionStart(System::SharedPtr<Aspose::Words::Section>)Called when enumeration of a section has started.
virtual VisitShapeEnd(System::SharedPtr<Aspose::Words::Drawing::Shape>)Called when enumeration of a shape has ended.
virtual VisitShapeStart(System::SharedPtr<Aspose::Words::Drawing::Shape>)Called when enumeration of a shape has started.
virtual VisitSmartTagEnd(System::SharedPtr<Aspose::Words::Markup::SmartTag>)Called when enumeration of a smart tag has ended.
virtual VisitSmartTagStart(System::SharedPtr<Aspose::Words::Markup::SmartTag>)Called when enumeration of a smart tag has started.
virtual VisitSpecialChar(System::SharedPtr<Aspose::Words::SpecialChar>)Called when a SpecialChar node is encountered in the document.
virtual VisitStructuredDocumentTagEnd(System::SharedPtr<Aspose::Words::Markup::StructuredDocumentTag>)Called when enumeration of a structured document tag has ended.
virtual VisitStructuredDocumentTagRangeEnd(System::SharedPtr<Aspose::Words::Markup::StructuredDocumentTagRangeEnd>)Called when a StructuredDocumentTagRangeEnd is encountered.
virtual VisitStructuredDocumentTagRangeStart(System::SharedPtr<Aspose::Words::Markup::StructuredDocumentTagRangeStart>)Called when a StructuredDocumentTagRangeStart is encountered.
virtual VisitStructuredDocumentTagStart(System::SharedPtr<Aspose::Words::Markup::StructuredDocumentTag>)Called when enumeration of a structured document tag has started.
virtual VisitSubDocument(System::SharedPtr<Aspose::Words::SubDocument>)Called when a sub-document is encountered.
virtual VisitTableEnd(System::SharedPtr<Aspose::Words::Tables::Table>)Called when enumeration of a table has ended.
virtual VisitTableStart(System::SharedPtr<Aspose::Words::Tables::Table>)Called when enumeration of a table has started.

Remarks

With DocumentVisitor you can define and execute custom operations that require enumeration over the document tree.

For example, Aspose.Words uses DocumentVisitor internally for saving Document in various formats and for other operations like finding fields or bookmarks over a fragment of a document.

To use DocumentVisitor:

  1. Create a class derived from DocumentVisitor.
  2. Override and provide implementations for some or all of the VisitXXX methods to perform some custom operations.
  3. Call Node.Accept on the Node that you want to start the enumeration from.

DocumentVisitor provides default implementations for all of the VisitXXX methods to make it easier to create new document visitors as only the methods required for the particular visitor need to be overridden. It is not necessary to override all of the visitor methods.

For more information see the Visitor design pattern.

Examples

Shows how to use a document visitor to print a document’s node structure.

void DocStructureToText()
{
    auto doc = MakeObject<Document>(MyDir + u"DocumentVisitor-compatible features.docx");
    auto visitor = MakeObject<ExDocumentVisitor::DocStructurePrinter>();

    // When we get a composite node to accept a document visitor, the visitor visits the accepting node,
    // and then traverses all the node's children in a depth-first manner.
    // The visitor can read and modify each visited node.
    doc->Accept(visitor);

    std::cout << visitor->GetText() << std::endl;
}

class DocStructurePrinter : public DocumentVisitor
{
public:
    DocStructurePrinter() : mDocTraversalDepth(0)
    {
        mAcceptingNodeChildTree = MakeObject<System::Text::StringBuilder>();
    }

    String GetText()
    {
        return mAcceptingNodeChildTree->ToString();
    }

    VisitorAction VisitDocumentStart(SharedPtr<Document> doc) override
    {
        int childNodeCount = doc->GetChildNodes(NodeType::Any, true)->get_Count();

        IndentAndAppendLine(String(u"[Document start] Child nodes: ") + childNodeCount);
        mDocTraversalDepth++;

        // Allow the visitor to continue visiting other nodes.
        return VisitorAction::Continue;
    }

    VisitorAction VisitDocumentEnd(SharedPtr<Document> doc) override
    {
        mDocTraversalDepth--;
        IndentAndAppendLine(u"[Document end]");

        return VisitorAction::Continue;
    }

    VisitorAction VisitSectionStart(SharedPtr<Section> section) override
    {
        // Get the index of our section within the document.
        SharedPtr<NodeCollection> docSections = section->get_Document()->GetChildNodes(NodeType::Section, false);
        int sectionIndex = docSections->IndexOf(section);

        IndentAndAppendLine(String(u"[Section start] Section index: ") + sectionIndex);
        mDocTraversalDepth++;

        return VisitorAction::Continue;
    }

    VisitorAction VisitSectionEnd(SharedPtr<Section> section) override
    {
        mDocTraversalDepth--;
        IndentAndAppendLine(u"[Section end]");

        return VisitorAction::Continue;
    }

    VisitorAction VisitBodyStart(SharedPtr<Body> body) override
    {
        int paragraphCount = body->get_Paragraphs()->get_Count();
        IndentAndAppendLine(String(u"[Body start] Paragraphs: ") + paragraphCount);
        mDocTraversalDepth++;

        return VisitorAction::Continue;
    }

    VisitorAction VisitBodyEnd(SharedPtr<Body> body) override
    {
        mDocTraversalDepth--;
        IndentAndAppendLine(u"[Body end]");

        return VisitorAction::Continue;
    }

    VisitorAction VisitParagraphStart(SharedPtr<Paragraph> paragraph) override
    {
        IndentAndAppendLine(u"[Paragraph start]");
        mDocTraversalDepth++;

        return VisitorAction::Continue;
    }

    VisitorAction VisitParagraphEnd(SharedPtr<Paragraph> paragraph) override
    {
        mDocTraversalDepth--;
        IndentAndAppendLine(u"[Paragraph end]");

        return VisitorAction::Continue;
    }

    VisitorAction VisitRun(SharedPtr<Run> run) override
    {
        IndentAndAppendLine(String(u"[Run] \"") + run->GetText() + u"\"");

        return VisitorAction::Continue;
    }

    VisitorAction VisitSubDocument(SharedPtr<SubDocument> subDocument) override
    {
        IndentAndAppendLine(u"[SubDocument]");

        return VisitorAction::Continue;
    }

private:
    int mDocTraversalDepth;
    SharedPtr<System::Text::StringBuilder> mAcceptingNodeChildTree;

    void IndentAndAppendLine(String text)
    {
        for (int i = 0; i < mDocTraversalDepth; i++)
        {
            mAcceptingNodeChildTree->Append(u"|  ");
        }

        mAcceptingNodeChildTree->AppendLine(text);
    }
};

See Also