FindReplaceOptionsUseLegacyOrder Property

True indicates that a text search is performed sequentially from top to bottom considering the text boxes. Default value is false.

Namespace:  Aspose.Words.Replacing
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.10.0

ExpandedSyntax

public bool UseLegacyOrder { get; set; }

Property Value

Type: Boolean

ExpandedExamples

Shows how to change the searching order of nodes when performing a find-and-replace text operation.
public void UseLegacyOrder(bool isUseLegacyOrder)
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert three runs which can be used as tags, with the second placed inside a text box.
    builder.Writeln("[tag 1]");
    Shape textBox = builder.InsertShape(ShapeType.TextBox, 100, 50);
    builder.Writeln("[tag 3]");
    builder.MoveTo(textBox.FirstParagraph);
    builder.Write("[tag 2]");

    FindReplaceOptions options = new FindReplaceOptions();
    TextReplacementTracker callback = new TextReplacementTracker();
    options.ReplacingCallback = callback;

    // When a text replacement is performed, all of the runs of a document have their contents searched
    // for every instance of the string that we wish to replace.
    // This flag can change the search priority of runs inside text boxes.
    options.UseLegacyOrder = isUseLegacyOrder;

    doc.Range.Replace(new Regex(@"\[tag \d*\]"), "", options);

    // Using legacy order goes through all runs of a range in sequential order.
    // Not using legacy order goes through runs within text boxes after all runs outside of text boxes have been searched.
    Assert.AreEqual(isUseLegacyOrder ?
        new List<string> { "[tag 1]", "[tag 2]", "[tag 3]" } :
        new List<string> { "[tag 1]", "[tag 3]", "[tag 2]" }, callback.Matches);
}

/// <summary>
/// Creates a list of string matches from a regex-based text find-and-replacement operation
/// in the order that they are encountered.
/// </summary>
private class TextReplacementTracker : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        Matches.Add(e.Match.Value);
        return ReplaceAction.Replace;
    }

    public List<string> Matches { get; } = new List<string>();
}

ExpandedSee Also