FindReplaceOptionsDirection Property

Selects direction for replace. Default value is Forward.

Namespace:  Aspose.Words.Replacing
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public FindReplaceDirection Direction { get; set; }

Property Value

Type: FindReplaceDirection
Examples
Shows how to apply a different font to new content via FindReplaceOptions.
public void ReplaceNumbersAsHex()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.Font.Name = "Arial";
    builder.Write(
        "There are few numbers that should be converted to HEX and highlighted: 123, 456, 789 and 17379.");

    FindReplaceOptions options = new FindReplaceOptions();

    // Highlight newly inserted content with a color
    options.ApplyFont.HighlightColor = Color.LightGray;

    // Apply an IReplacingCallback to make the replacement to convert integers into hex equivalents
    // and also to count replacements in the order they take place
    options.ReplacingCallback = new NumberHexer();

    // By default, text is searched for replacements front to back, but we can change it to go the other way
    options.Direction = FindReplaceDirection.Backward;

    int count = doc.Range.Replace(new Regex("[0-9]+"), "", options);
    Assert.AreEqual(4, count);

    doc.Save(ArtifactsDir + "Range.ReplaceNumbersAsHex.docx");
}

/// <summary>
/// Replaces arabic numbers with hexadecimal equivalents and appends the number of each replacement.
/// </summary>
private class NumberHexer : IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        mCurrentReplacementNumber++;

        // Parse numbers
        int number = Convert.ToInt32(args.Match.Value);

        // And write it as HEX
        args.Replacement = $"0x{number:X} (replacement #{mCurrentReplacementNumber})";

        Console.WriteLine($"Match #{mCurrentReplacementNumber}");
        Console.WriteLine($"\tOriginal value:\t{args.Match.Value}");
        Console.WriteLine($"\tReplacement:\t{args.Replacement}");
        Console.WriteLine($"\tOffset in parent {args.MatchNode.NodeType} node:\t{args.MatchOffset}");

        Console.WriteLine(string.IsNullOrEmpty(args.GroupName)
            ? $"\tGroup index:\t{args.GroupIndex}"
            : $"\tGroup name:\t{args.GroupName}");

        return ReplaceAction.Replace;
    }

    private int mCurrentReplacementNumber;
}
See Also