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.
Example:
Shows how to insert content of one document into another during a customized find and replace operation.
public void insertDocumentAtReplace() throws Exception {
Document mainDoc = new Document(getMyDir() + "Document insertion destination.docx");
FindReplaceOptions options = new FindReplaceOptions();
options.setDirection(FindReplaceDirection.BACKWARD);
options.setReplacingCallback(new InsertDocumentAtReplaceHandler());
mainDoc.getRange().replace("[MY_DOCUMENT]", "", options);
mainDoc.save(getArtifactsDir() + "InsertDocument.InsertDocumentAtReplace.docx");
}
private static class InsertDocumentAtReplaceHandler implements IReplacingCallback {
public /*ReplaceAction*/int /*IReplacingCallback.*/replacing(ReplacingArgs args) throws Exception {
Document subDoc = new Document(getMyDir() + "Document.docx");
// Insert a document after the paragraph, containing the match text
Paragraph para = (Paragraph) args.getMatchNode().getParentNode();
insertDocument(para, subDoc);
// Remove the paragraph with the match text
para.remove();
return ReplaceAction.SKIP;
}
}
/// <summary>
/// Inserts content of the external document after the specified node.
/// </summary>
static void insertDocument(Node insertionDestination, Document docToInsert) {
// Make sure that the node is either a paragraph or table
if (((insertionDestination.getNodeType()) == (NodeType.PARAGRAPH)) || ((insertionDestination.getNodeType()) == (NodeType.TABLE))) {
// We will be inserting into the parent of the destination paragraph
CompositeNode dstStory = insertionDestination.getParentNode();
// This object will be translating styles and lists during the import
NodeImporter importer =
new NodeImporter(docToInsert, insertionDestination.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Loop through all block level nodes in the body of the section
for (Section srcSection : docToInsert.getSections())
for (Node srcNode : srcSection.getBody()) {
// Let's skip the node if it is a last empty paragraph in a section
if (((srcNode.getNodeType()) == (NodeType.PARAGRAPH))) {
Paragraph para = (Paragraph) srcNode;
if (para.isEndOfSection() && !para.hasChildNodes())
continue;
}
// This creates a clone of the node, suitable for insertion into the destination document
Node newNode = importer.importNode(srcNode, true);
// Insert new node after the reference node
dstStory.insertAfter(newNode, insertionDestination);
insertionDestination = newNode;
}
} else {
throw new IllegalArgumentException("The destination node should 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 int | REPLACE = 0 | |
Replace the current match.
|
static final int | SKIP = 1 | |
Skip the current match.
|
static final int | STOP = 2 | |
Terminate the replace operation.
|
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.
See Also:
Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
Aspose.Words Support Forum - our preferred method of support.