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 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("[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 matched text.
Paragraph para = (Paragraph)args.getMatchNode().getParentNode();
insertDocument(para, subDoc);
// Remove the paragraph with the matched text.
para.remove();
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())
continue;
}
// Clone the node, and insert it into the destination document.
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 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.