search/mag_sel search/close
Aspose::Words::Replacing Namespace Reference

The Aspose.Words.Replacing namespace provides classes to manipulate with find/replace operations over the document tree.

Classes

class  FindReplaceOptions
 Specifies options for find/replace operations. More...
 
interface  IReplacingCallback
 Implement this interface if you want to have your own custom method called during a find and replace operation. More...
 
class  ReplacingArgs
 Provides data for a custom replace operation. More...
 

Enumerations

enum class  FindReplaceDirection
 Specifies direction for replace operations. More...
 
enum class  ReplaceAction
 Allows the user to specify what happens to the current match during a replace operation. More...
 

Enumeration Type Documentation

◆ FindReplaceDirection

Specifies direction for replace operations.

Examples

Shows how to determine which direction a find-and-replace operation traverses the document in.

void Direction(FindReplaceDirection findReplaceDirection)
{
auto doc = MakeObject<Document>();
auto builder = MakeObject<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(u"Match 1.");
builder->Writeln(u"Match 2.");
builder->Writeln(u"Match 3.");
builder->Writeln(u"Match 4.");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
// Assign a custom callback to the "ReplacingCallback" property.
auto callback = MakeObject<ExRange::TextReplacementRecorder>();
options->set_ReplacingCallback(callback);
// Set the "Direction" property to "FindReplaceDirection.Backward" to get the find-and-replace
// operation to start from the end of the range, and traverse back to the beginning.
// Set the "Direction" property to "FindReplaceDirection.Backward" to get the find-and-replace
// operation to start from the beginning of the range, and traverse to the end.
options->set_Direction(findReplaceDirection);
doc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"Match \\d*"), u"Replacement", options);
ASSERT_EQ(String(u"Replacement.\r") + u"Replacement.\r" + u"Replacement.\r" + u"Replacement.", doc->GetText().Trim());
switch (findReplaceDirection)
{
ASPOSE_ASSERT_EQ(MakeArray<String>({u"Match 1", u"Match 2", u"Match 3", u"Match 4"}), callback->get_Matches());
break;
ASPOSE_ASSERT_EQ(MakeArray<String>({u"Match 4", u"Match 3", u"Match 2", u"Match 1"}), callback->get_Matches());
break;
}
}
class TextReplacementRecorder : public IReplacingCallback
{
public:
SharedPtr<System::Collections::Generic::List<String>> get_Matches()
{
return mMatches;
}
ReplaceAction Replacing(SharedPtr<ReplacingArgs> e) override
{
get_Matches()->Add(e->get_Match()->get_Value());
}
TextReplacementRecorder() : mMatches(MakeObject<System::Collections::Generic::List<String>>())
{
}
private:
SharedPtr<System::Collections::Generic::List<String>> mMatches;
};
Enumerator
Forward 

Matched items are replaced from first to last.

Backward 

Matched items are replaced from last back to first.

◆ ReplaceAction

Allows the user to specify what happens to the current match during a replace operation.

See also
Aspose::Words::Replacing::IReplacingCallback
Aspose::Words::Range
Aspose::Words::Range::Replace(System::String, System::String, System::SharedPtr<Aspose::Words::Replacing::FindReplaceOptions>)
Examples

Shows how to insert an entire document's contents as a replacement of a match in a find-and-replace operation.

void InsertDocumentAtReplace()
{
auto mainDoc = MakeObject<Document>(MyDir + u"Document insertion destination.docx");
// We can use a "FindReplaceOptions" object to modify the find-and-replace process.
auto options = MakeObject<FindReplaceOptions>();
options->set_ReplacingCallback(MakeObject<ExRange::InsertDocumentAtReplaceHandler>());
mainDoc->get_Range()->Replace(MakeObject<System::Text::RegularExpressions::Regex>(u"\\[MY_DOCUMENT\\]"), u"", options);
mainDoc->Save(ArtifactsDir + u"InsertDocument.InsertDocumentAtReplace.docx");
}
class InsertDocumentAtReplaceHandler : public IReplacingCallback
{
public:
ReplaceAction Replacing(SharedPtr<ReplacingArgs> args) override
{
auto subDoc = MakeObject<Document>(MyDir + u"Document.docx");
// Insert a document after the paragraph containing the matched text.
auto para = System::DynamicCast<Paragraph>(args->get_MatchNode()->get_ParentNode());
InsertDocument(para, subDoc);
// Remove the paragraph with the matched text.
para->Remove();
}
};
static void InsertDocument(SharedPtr<Node> insertionDestination, SharedPtr<Document> docToInsert)
{
if (insertionDestination->get_NodeType() == NodeType::Paragraph || insertionDestination->get_NodeType() == NodeType::Table)
{
SharedPtr<CompositeNode> dstStory = insertionDestination->get_ParentNode();
auto importer = MakeObject<NodeImporter>(docToInsert, insertionDestination->get_Document(), ImportFormatMode::KeepSourceFormatting);
for (const auto& srcSection : System::IterateOver(docToInsert->get_Sections()->LINQ_OfType<SharedPtr<Section>>()))
{
for (const auto& srcNode : System::IterateOver(srcSection->get_Body()))
{
// Skip the node if it is the last empty paragraph in a section.
if (srcNode->get_NodeType() == NodeType::Paragraph)
{
auto para = System::DynamicCast<Paragraph>(srcNode);
if (para->get_IsEndOfSection() && !para->get_HasChildNodes())
{
continue;
}
}
SharedPtr<Node> newNode = importer->ImportNode(srcNode, true);
dstStory->InsertAfter(newNode, insertionDestination);
insertionDestination = newNode;
}
}
}
else
{
throw System::ArgumentException(u"The destination node must be either a paragraph or table.");
}
}
Enumerator
Replace 

Replace the current match.

Skip 

Skip the current match.

Stop 

Terminate the replace operation.