ReplaceAction

Inheritance: java.lang.Object

public class ReplaceAction

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

Examples:

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


 public void insertDocumentAtReplace() throws Exception {
     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);
     mainDoc.save(getArtifactsDir() + "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.
         para.remove();

         return ReplaceAction.SKIP;
     }
 }

 /// 
 /// Inserts all the nodes of another document after a paragraph or table.
 /// 
 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())
                         continue;
                 }

                 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.");
     }
 }
 

Fields

FieldDescription
REPLACEReplace the current match.
SKIPSkip the current match.
STOPTerminate the replace operation.
length

Methods

MethodDescription
fromName(String replaceActionName)
getName(int replaceAction)
getValues()
toString(int replaceAction)

REPLACE

public static int REPLACE

Replace the current match.

SKIP

public static int SKIP

Skip the current match.

STOP

public static int STOP

Terminate the replace operation.

length

public static int length

fromName(String replaceActionName)

public static int fromName(String replaceActionName)

Parameters:

ParameterTypeDescription
replaceActionNamejava.lang.String

Returns: int

getName(int replaceAction)

public static String getName(int replaceAction)

Parameters:

ParameterTypeDescription
replaceActionint

Returns: java.lang.String

getValues()

public static int[] getValues()

Returns: int[]

toString(int replaceAction)

public static String toString(int replaceAction)

Parameters:

ParameterTypeDescription
replaceActionint

Returns: java.lang.String