DocumentVisitor Class |
Namespace: Aspose.Words
The DocumentVisitor type exposes the following members.
| Name | Description | |
|---|---|---|
| Equals | (Inherited from Object.) | |
| GetHashCode | (Inherited from Object.) | |
| GetType | (Inherited from Object.) | |
| ToString | (Inherited from Object.) | |
| VisitAbsolutePositionTab |
Called when a AbsolutePositionTab node is encountered in the document.
| |
| VisitBodyEnd |
Called when enumeration of the main text story in a section has ended.
| |
| VisitBodyStart |
Called when enumeration of the main text story in a section has started.
| |
| VisitBookmarkEnd |
Called when an end of a bookmark is encountered in the document.
| |
| VisitBookmarkStart |
Called when a start of a bookmark is encountered in the document.
| |
| VisitBuildingBlockEnd |
Called when enumeration of a building block has ended.
| |
| VisitBuildingBlockStart |
Called when enumeration of a building block has started.
| |
| VisitCellEnd |
Called when enumeration of a table cell has ended.
| |
| VisitCellStart |
Called when enumeration of a table cell has started.
| |
| VisitCommentEnd |
Called when enumeration of a comment text has ended.
| |
| VisitCommentRangeEnd |
Called when the end of a commented range of text is encountered.
| |
| VisitCommentRangeStart |
Called when the start of a commented range of text is encountered.
| |
| VisitCommentStart |
Called when enumeration of a comment text has started.
| |
| VisitDocumentEnd |
Called when enumeration of the document has finished.
| |
| VisitDocumentStart |
Called when enumeration of the document has started.
| |
| VisitEditableRangeEnd |
Called when an end of an editable range is encountered in the document.
| |
| VisitEditableRangeStart |
Called when a start of an editable range is encountered in the document.
| |
| VisitFieldEnd |
Called when a field ends in the document.
| |
| VisitFieldSeparator |
Called when a field separator is encountered in the document.
| |
| VisitFieldStart |
Called when a field starts in the document.
| |
| VisitFootnoteEnd |
Called when enumeration of a footnote or endnote text has ended.
| |
| VisitFootnoteStart |
Called when enumeration of a footnote or endnote text has started.
| |
| VisitFormField |
Called when a form field is encountered in the document.
| |
| VisitGlossaryDocumentEnd |
Called when enumeration of a glossary document has ended.
| |
| VisitGlossaryDocumentStart |
Called when enumeration of a glossary document has started.
| |
| VisitGroupShapeEnd |
Called when enumeration of a group shape has ended.
| |
| VisitGroupShapeStart |
Called when enumeration of a group shape has started.
| |
| VisitHeaderFooterEnd |
Called when enumeration of a header or footer in a section has ended.
| |
| VisitHeaderFooterStart |
Called when enumeration of a header or footer in a section has started.
| |
| VisitOfficeMathEnd |
Called when enumeration of a Office Math object has ended.
| |
| VisitOfficeMathStart |
Called when enumeration of a Office Math object has started.
| |
| VisitParagraphEnd |
Called when enumeration of a paragraph has ended.
| |
| VisitParagraphStart |
Called when enumeration of a paragraph has started.
| |
| VisitRowEnd |
Called when enumeration of a table row has ended.
| |
| VisitRowStart |
Called when enumeration of a table row has started.
| |
| VisitRun |
Called when a run of text in the is encountered.
| |
| VisitSectionEnd |
Called when enumeration of a section has ended.
| |
| VisitSectionStart |
Called when enumeration of a section has started.
| |
| VisitShapeEnd |
Called when enumeration of a shape has ended.
| |
| VisitShapeStart |
Called when enumeration of a shape has started.
| |
| VisitSmartTagEnd |
Called when enumeration of a smart tag has ended.
| |
| VisitSmartTagStart |
Called when enumeration of a smart tag has started.
| |
| VisitSpecialChar |
Called when a SpecialChar node is encountered in the document.
| |
| VisitStructuredDocumentTagEnd |
Called when enumeration of a structured document tag has ended.
| |
| VisitStructuredDocumentTagStart |
Called when enumeration of a structured document tag has started.
| |
| VisitSubDocument |
Called when a subDocument is encountered.
| |
| VisitTableEnd |
Called when enumeration of a table has ended.
| |
| VisitTableStart |
Called when enumeration of a table has started.
|
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:
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.
public void DocStructureToText() { // Open the document that has nodes we want to print the info of Document doc = new Document(MyDir + "DocumentVisitor-compatible features.docx"); // Create an object that inherits from the DocumentVisitor class DocStructurePrinter visitor = new DocStructurePrinter(); // Accepting a visitor lets it start traversing the nodes in the document, // starting with the node that accepted it to then recursively visit every child doc.Accept(visitor); // Once the visiting is complete, we can retrieve the result of the operation, // that in this example, has accumulated in the visitor Console.WriteLine(visitor.GetText()); } /// <summary> /// This Visitor implementation prints information about sections, bodies, paragraphs and runs encountered in the document. /// </summary> public class DocStructurePrinter : DocumentVisitor { public DocStructurePrinter() { 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> /// Called when a Document node is encountered. /// </summary> public override VisitorAction VisitDocumentStart(Document doc) { int childNodeCount = doc.GetChildNodes(NodeType.Any, true).Count; // A Document node is at the root of every document, so if we let a document accept a visitor, this will be the first visitor action to be carried out IndentAndAppendLine("[Document start] Child nodes: " + childNodeCount); mDocTraversalDepth++; // Let the visitor continue visiting other nodes return VisitorAction.Continue; } /// <summary> /// Called when the visiting of a Document is ended. /// </summary> public override VisitorAction VisitDocumentEnd(Document doc) { // If we let a document accept a visitor, this will be the last visitor action to be carried out mDocTraversalDepth--; IndentAndAppendLine("[Document end]"); return VisitorAction.Continue; } /// <summary> /// Called when a Section node is encountered in the document. /// </summary> public override VisitorAction VisitSectionStart(Section section) { // Get the index of our section within the document NodeCollection docSections = section.Document.GetChildNodes(NodeType.Section, false); int sectionIndex = docSections.IndexOf(section); IndentAndAppendLine("[Section start] Section index: " + sectionIndex); mDocTraversalDepth++; return VisitorAction.Continue; } /// <summary> /// Called when the visiting of a Section node is ended. /// </summary> public override VisitorAction VisitSectionEnd(Section section) { mDocTraversalDepth--; IndentAndAppendLine("[Section end]"); return VisitorAction.Continue; } /// <summary> /// Called when a Body node is encountered in the document. /// </summary> public override VisitorAction VisitBodyStart(Body body) { int paragraphCount = body.Paragraphs.Count; IndentAndAppendLine("[Body start] Paragraphs: " + paragraphCount); mDocTraversalDepth++; return VisitorAction.Continue; } /// <summary> /// Called when the visiting of a Body node is ended. /// </summary> public override VisitorAction VisitBodyEnd(Body body) { mDocTraversalDepth--; IndentAndAppendLine("[Body end]"); return VisitorAction.Continue; } /// <summary> /// Called when a Paragraph node is encountered in the document. /// </summary> public override VisitorAction VisitParagraphStart(Paragraph paragraph) { IndentAndAppendLine("[Paragraph start]"); mDocTraversalDepth++; return VisitorAction.Continue; } /// <summary> /// Called when the visiting of a Paragraph node is ended. /// </summary> public override VisitorAction VisitParagraphEnd(Paragraph paragraph) { mDocTraversalDepth--; IndentAndAppendLine("[Paragraph end]"); return VisitorAction.Continue; } /// <summary> /// Called when a Run node is encountered in the document. /// </summary> public override VisitorAction VisitRun(Run run) { IndentAndAppendLine("[Run] \"" + run.GetText() + "\""); return VisitorAction.Continue; } /// <summary> /// Called when a SubDocument node is encountered in the document. /// </summary> public override VisitorAction VisitSubDocument(SubDocument subDocument) { IndentAndAppendLine("[SubDocument]"); return VisitorAction.Continue; } /// <summary> /// Append a line to the StringBuilder and indent it depending on how deep the visitor is into the document tree. /// </summary> /// <param name="text"></param> private void IndentAndAppendLine(string text) { for (int i = 0; i < mDocTraversalDepth; i++) mBuilder.Append("| "); mBuilder.AppendLine(text); } private int mDocTraversalDepth; private readonly StringBuilder mBuilder; }