DocumentVisitorVisitEditableRangeStart Method

Called when a start of an editable range is encountered in the document.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public virtual VisitorAction VisitEditableRangeStart(
	EditableRangeStart editableRangeStart
)

Parameters

editableRangeStart
Type: Aspose.WordsEditableRangeStart
The object that is being visited.

Return Value

Type: VisitorAction
A VisitorAction value that specifies how to continue the enumeration.
Examples
Traverse a document with a visitor that prints all editable ranges that it encounters.
public void EditableRangeToText()
{
    // Open the document that has editable ranges 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
    EditableRangeInfoPrinter visitor = new EditableRangeInfoPrinter();

    // 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);

    Paragraph p = new Paragraph(doc);
    p.AppendChild(new Run(doc, "Paragraph with editable range text."));

    // 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 editable ranges encountered in the document.
/// </summary>
public class EditableRangeInfoPrinter : DocumentVisitor
{
    public EditableRangeInfoPrinter()
    {
        mBuilder = new StringBuilder();
        mVisitorIsInsideEditableRange = false;
    }

    /// <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 Run node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitRun(Run run)
    {
        // We want to print the contents of runs, but only if they are inside shapes, as they would be in the case of text boxes
        if (mVisitorIsInsideEditableRange) IndentAndAppendLine("[Run] \"" + run.GetText() + "\"");

        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when an EditableRange node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitEditableRangeStart(EditableRangeStart editableRangeStart)
    {
        IndentAndAppendLine("[EditableRange start] ID: " + editableRangeStart.Id + " Owner: " +
                            editableRangeStart.EditableRange.SingleUser);
        mDocTraversalDepth++;
        mVisitorIsInsideEditableRange = true;

        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when the visiting of a EditableRange node is ended.
    /// </summary>
    public override VisitorAction VisitEditableRangeEnd(EditableRangeEnd editableRangeEnd)
    {
        mDocTraversalDepth--;
        IndentAndAppendLine("[EditableRange end]");
        mVisitorIsInsideEditableRange = false;

        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 bool mVisitorIsInsideEditableRange;
    private int mDocTraversalDepth;
    private readonly StringBuilder mBuilder;
}
See Also