//ExStart //ExFor:FindReplaceOptions.UseLegacyOrder //ExSummary: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); }
//ExStart //ExFor:FindReplaceOptions.UseLegacyOrder //ExSummary:Shows how to change the searching order of nodes when performing a find-and-replace text operation. 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.MoveTo(textBox.FirstParagraph); 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); }