FindReplaceOptions.UseLegacyOrder property

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

public bool UseLegacyOrder { get; set; }


Shows how to change the searching order of nodes when performing a find-and-replace text operation.

[TestCase(true)] // ExSkip
[TestCase(false)] // ExSkip
public void UseLegacyOrder(bool useLegacyOrder)
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert three runs which we can search for using a regex pattern.
    // Place one of those runs inside a text box.
    builder.Writeln("[tag 1]");
    Shape textBox = builder.InsertShape(ShapeType.TextBox, 100, 50);
    builder.Writeln("[tag 2]");
    builder.Write("[tag 3]");

    // We can use a "FindReplaceOptions" object to modify the find-and-replace process.
    FindReplaceOptions options = new FindReplaceOptions();

    // Assign a custom callback to the "ReplacingCallback" property.
    TextReplacementTracker callback = new TextReplacementTracker();
    options.ReplacingCallback = callback;

    // If we set the "UseLegacyOrder" property to "true", the
    // find-and-replace operation will go through all the runs outside of a text box
    // before going through the ones inside a text box.
    // If we set the "UseLegacyOrder" property to "false", the
    // find-and-replace operation will go over all the runs in a range in sequential order.
    options.UseLegacyOrder = useLegacyOrder;

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

    Assert.AreEqual(useLegacyOrder ?
        new List<string> { "[tag 1]", "[tag 3]", "[tag 2]" } :
        new List<string> { "[tag 1]", "[tag 2]", "[tag 3]" }, callback.Matches);

/// <summary>
/// Records the order of all matches that occur during a find-and-replace operation.
/// </summary>
private class TextReplacementTracker : IReplacingCallback
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
        return ReplaceAction.Replace;

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

See Also