VisitParagraphEnd
Contents
[
Hide
]DocumentVisitor.VisitParagraphEnd method
Called when enumeration of a paragraph has ended.
public virtual VisitorAction VisitParagraphEnd(Paragraph paragraph)
Parameter | Type | Description |
---|---|---|
paragraph | Paragraph | The object that is being visited. |
Return Value
A VisitorAction
value that specifies how to continue the enumeration.
Examples
Shows how to use a document visitor to print a document’s node structure.
public void DocStructureToText()
{
Document doc = new Document(MyDir + "DocumentVisitor-compatible features.docx");
DocStructurePrinter visitor = new 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);
Console.WriteLine(visitor.GetText());
}
/// <summary>
/// Traverses a node's tree of child nodes.
/// Creates a map of this tree in the form of a string.
/// </summary>
public class DocStructurePrinter : DocumentVisitor
{
public DocStructurePrinter()
{
mAcceptingNodeChildTree = new StringBuilder();
}
public string GetText()
{
return mAcceptingNodeChildTree.ToString();
}
/// <summary>
/// Called when a Document node is encountered.
/// </summary>
public override VisitorAction VisitDocumentStart(Document doc)
{
int childNodeCount = doc.GetChildNodes(NodeType.Any, true).Count;
IndentAndAppendLine("[Document start] Child nodes: " + childNodeCount);
mDocTraversalDepth++;
// Allow the visitor to continue visiting other nodes.
return VisitorAction.Continue;
}
/// <summary>
/// Called after all the child nodes of a Document node have been visited.
/// </summary>
public override VisitorAction VisitDocumentEnd(Document doc)
{
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 after all the child nodes of a Section node have been visited.
/// </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 after all the child nodes of a Body node have been visited.
/// </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 after all the child nodes of a Paragraph node have been visited.
/// </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>
/// Called when a SubDocument node is encountered in the document.
/// </summary>
public override VisitorAction VisitStructuredDocumentTagRangeStart(StructuredDocumentTagRangeStart sdtRangeStart)
{
IndentAndAppendLine("[SdtRangeStart]");
return VisitorAction.Continue;
}
/// <summary>
/// Called when a SubDocument node is encountered in the document.
/// </summary>
public override VisitorAction VisitStructuredDocumentTagRangeEnd(StructuredDocumentTagRangeEnd sdtRangeEnd)
{
IndentAndAppendLine("[SdtRangeEnd]");
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++) mAcceptingNodeChildTree.Append("| ");
mAcceptingNodeChildTree.AppendLine(text);
}
private int mDocTraversalDepth;
private readonly StringBuilder mAcceptingNodeChildTree;
}
See Also
- enum VisitorAction
- class Paragraph
- class DocumentVisitor
- namespace Aspose.Words
- assembly Aspose.Words