com.aspose.words

Class FindReplaceDirection

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

Utility class containing constants. Specifies direction for replace operations.

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

Field Summary
static final intFORWARD = 0
Matched items are replaced from first to last.
static final intBACKWARD = 1
Matched items are replaced from last back to first.
 

    • Field Detail

      • FORWARD = 0

        public static final int FORWARD
        Matched items are replaced from first to last.
      • BACKWARD = 1

        public static final int BACKWARD
        Matched items are replaced from last back to first.