AcceptStart
Contents
[
Hide
]Body.AcceptStart method
Accepts a visitor for visiting the start of the document’s body.
public override VisitorAction AcceptStart(DocumentVisitor visitor)
Parameter | Type | Description |
---|---|---|
visitor | DocumentVisitor | The document visitor. |
Return Value
The action to be taken by the visitor.
Examples
Shows how to process absolute position tab characters with a document visitor.
public void DocumentToTxt()
{
Document doc = new Document(MyDir + "Absolute position tab.docx");
// Extract the text contents of our document by accepting this custom document visitor.
DocTextExtractor myDocTextExtractor = new DocTextExtractor();
Section fisrtSection = doc.FirstSection;
fisrtSection.Body.Accept(myDocTextExtractor);
// Visit only start of the document body.
fisrtSection.Body.AcceptStart(myDocTextExtractor);
// Visit only end of the document body.
fisrtSection.Body.AcceptEnd(myDocTextExtractor);
// The absolute position tab, which has no equivalent in string form, has been explicitly converted to a tab character.
Assert.AreEqual("Before AbsolutePositionTab\tAfter AbsolutePositionTab", myDocTextExtractor.GetText());
// An AbsolutePositionTab can accept a DocumentVisitor by itself too.
AbsolutePositionTab absPositionTab = (AbsolutePositionTab)doc.FirstSection.Body.FirstParagraph.GetChild(NodeType.SpecialChar, 0, true);
myDocTextExtractor = new DocTextExtractor();
absPositionTab.Accept(myDocTextExtractor);
Assert.AreEqual("\t", myDocTextExtractor.GetText());
}
/// <summary>
/// Collects the text contents of all runs in the visited document. Replaces all absolute tab characters with ordinary tabs.
/// </summary>
public class DocTextExtractor : DocumentVisitor
{
public DocTextExtractor()
{
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public override VisitorAction VisitRun(Run run)
{
AppendText(run.Text);
return VisitorAction.Continue;
}
/// <summary>
/// Called when an AbsolutePositionTab node is encountered in the document.
/// </summary>
public override VisitorAction VisitAbsolutePositionTab(AbsolutePositionTab tab)
{
mBuilder.Append("\t");
return VisitorAction.Continue;
}
/// <summary>
/// Adds text to the current output. Honors the enabled/disabled output flag.
/// </summary>
private void AppendText(string text)
{
mBuilder.Append(text);
}
/// <summary>
/// Plain text of the document that was accumulated by the visitor.
/// </summary>
public string GetText()
{
return mBuilder.ToString();
}
private readonly StringBuilder mBuilder;
}
See Also
- enum VisitorAction
- class DocumentVisitor
- class Body
- namespace Aspose.Words
- assembly Aspose.Words