Class ReplaceAction

  • java.lang.Object
    • com.aspose.words.ReplaceAction
public class ReplaceAction 
extends java.lang.Object

Utility class containing constants. Allows the user to specify what happens to the current match during a replace operation.


Shows how to insert an entire document's contents as a replacement of a match in a find-and-replace operation.
Document mainDoc = new Document(getMyDir() + "Document insertion destination.docx");

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

    mainDoc.getRange().replace(Pattern.compile("\\[MY_DOCUMENT\\]"), "", options); + "InsertDocument.InsertDocumentAtReplace.docx");

private static class InsertDocumentAtReplaceHandler implements IReplacingCallback {
    public int replacing(ReplacingArgs args) throws Exception {
        Document subDoc = new Document(getMyDir() + "Document.docx");

        // Insert a document after the paragraph containing the matched text.
        Paragraph para = (Paragraph) args.getMatchNode().getParentNode();
        insertDocument(para, subDoc);

        // Remove the paragraph with the matched text.

        return ReplaceAction.SKIP;

/// <summary>
/// Inserts all the nodes of another document after a paragraph or table.
/// </summary>
private static void insertDocument(Node insertionDestination, Document docToInsert) {
    if (((insertionDestination.getNodeType()) == (NodeType.PARAGRAPH)) || ((insertionDestination.getNodeType()) == (NodeType.TABLE))) {
        CompositeNode dstStory = insertionDestination.getParentNode();

        NodeImporter importer =
                new NodeImporter(docToInsert, insertionDestination.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);

        for (Section srcSection : docToInsert.getSections())
            for (Node srcNode : srcSection.getBody()) {
                // Skip the node if it is the last empty paragraph in a section.
                if (((srcNode.getNodeType()) == (NodeType.PARAGRAPH))) {
                    Paragraph para = (Paragraph) srcNode;
                    if (para.isEndOfSection() && !para.hasChildNodes())

                Node newNode = importer.importNode(srcNode, true);

                dstStory.insertAfter(newNode, insertionDestination);
                insertionDestination = newNode;
    } else {
        throw new IllegalArgumentException("The destination node must be either a paragraph or table.");
See Also:
IReplacingCallback, Range, Range.replace(java.lang.String,java.lang.String,com.aspose.words.FindReplaceOptions)

Field Summary
static final intREPLACE = 0
Replace the current match.
static final intSKIP = 1
Skip the current match.
static final intSTOP = 2
Terminate the replace operation.

    • Field Detail

      • REPLACE = 0

        public static final int REPLACE
        Replace the current match.
      • SKIP = 1

        public static final int SKIP
        Skip the current match.
      • STOP = 2

        public static final int STOP
        Terminate the replace operation.