ReplacingArgs Class

Provides data for a custom replace operation.
Inheritance Hierarchy
SystemObject
  Aspose.Words.ReplacingReplacingArgs

Namespace:  Aspose.Words.Replacing
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class ReplacingArgs

The ReplacingArgs type exposes the following members.

Properties
  NameDescription
Public propertyCode exampleGroupIndex
Identifies, by index, a captured group in the Match that is to be replaced with the Replacement string.
Public propertyCode exampleGroupName
Identifies, by name, a captured group in the Match that is to be replaced with the Replacement string.
Public propertyCode exampleMatch
The Match resulting from a single regular expression match during a Replace.
Public propertyCode exampleMatchNode
Gets the node that contains the beginning of the match.
Public propertyCode exampleMatchOffset
Gets the zero-based starting position of the match from the start of the node that contains the beginning of the match.
Public propertyCode exampleReplacement
Gets or sets the replacement string.
Methods
  NameDescription
Public methodEquals (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodToString (Inherited from Object.)
Remarks
See and its overloads.
Examples
Shows how to insert content of one document into another during a customized find and replace operation.
public void InsertDocumentAtReplace()
{
    Document mainDoc = new Document(MyDir + "Document insertion destination.docx");

    FindReplaceOptions options = new FindReplaceOptions();
    options.Direction = FindReplaceDirection.Backward;
    options.ReplacingCallback = new InsertDocumentAtReplaceHandler();

    mainDoc.Range.Replace(new Regex("\\[MY_DOCUMENT\\]"), "", options);
    mainDoc.Save(ArtifactsDir + "InsertDocument.InsertDocumentAtReplace.doc");
}

private class InsertDocumentAtReplaceHandler : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
    {
        Document subDoc = new Document(MyDir + "Document.docx");

        // Insert a document after the paragraph, containing the match text
        Paragraph para = (Paragraph) args.MatchNode.ParentNode;
        InsertDocument(para, subDoc);

        // Remove the paragraph with the match text
        para.Remove();

        return ReplaceAction.Skip;
    }
}
Examples
Replaces text specified with regular expression with HTML.
public void ReplaceWithInsertHtml()
{
    // Open the document
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.Writeln("Hello <CustomerName>,");

    FindReplaceOptions options = new FindReplaceOptions();
    options.ReplacingCallback = new ReplaceWithHtmlEvaluator(options);

    doc.Range.Replace(new Regex(@" <CustomerName>,"), string.Empty, options);

    // Save the modified document
    doc.Save(ArtifactsDir + "Range.ReplaceWithInsertHtml.doc");

}

private class ReplaceWithHtmlEvaluator : IReplacingCallback
{
    internal ReplaceWithHtmlEvaluator(FindReplaceOptions options)
    {
        mOptions = options;
    }

    /// <summary>
    /// NOTE: This is a simplistic method that will only work well when the match
    /// starts at the beginning of a run.
    /// </summary>
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
    {
        DocumentBuilder builder = new DocumentBuilder((Document) args.MatchNode.Document);
        builder.MoveTo(args.MatchNode);

        // Replace '<CustomerName>' text with a red bold name
        builder.InsertHtml("<b><font color='red'>James Bond, </font></b>");
        args.Replacement = "";

        return ReplaceAction.Replace;
    }

    private readonly FindReplaceOptions mOptions;
}
See Also