com.aspose.words
Interface IReplacingCallback


public interface IReplacingCallback 

Implement this interface if you want to have your own custom method called during a find and 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() + "InsertDocument1.doc");
    mainDoc.getRange().replace(Pattern.compile("\\[MY_DOCUMENT\\]"), new InsertDocumentAtReplaceHandler(), false);
    mainDoc.save(getMyDir() + "InsertDocumentAtReplace Out.doc");
}

private class InsertDocumentAtReplaceHandler implements IReplacingCallback
{
    public int replacing(ReplacingArgs e) throws Exception
    {
        Document subDoc = new Document(getMyDir() + "InsertDocument2.doc");

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

        // Remove the paragraph with the match text.
        para.remove();

        return ReplaceAction.SKIP;
    }
}

Example:

Replaces text specified with regular expression with HTML.
public void replaceWithInsertHtml() throws Exception
{
    // Open the document.
    Document doc = new Document(getMyDir() + "Range.ReplaceWithInsertHtml.doc");

    doc.getRange().replace(Pattern.compile("<CustomerName>"), new ReplaceWithHtmlEvaluator(), false);

    // Save the modified document.
    doc.save(getMyDir() + "Range.ReplaceWithInsertHtml Out.doc");

}

private class ReplaceWithHtmlEvaluator implements IReplacingCallback
{
    /**
     * NOTE: This is a simplistic method that will only work well when the match
     * starts at the beginning of a run.
     */
    public int replacing(ReplacingArgs e) throws Exception
    {
        DocumentBuilder builder = new DocumentBuilder((Document)e.getMatchNode().getDocument());
        builder.moveTo(e.getMatchNode());
        // Replace '<CustomerName>' text with a red bold name.
        builder.insertHtml("<b><font color='red'>James Bond</font></b>");

        e.setReplacement("");
        return ReplaceAction.REPLACE;
    }
}

Method Summary
abstract intreplacing(ReplacingArgs args)
           A user defined method that is called during a replace operation for each match found just before a replace is made.
 

Method Detail

replacing

public abstract int replacing(ReplacingArgs args)
                           throws java.lang.Exception
A user defined method that is called during a replace operation for each match found just before a replace is made.
Returns:
A ReplaceAction value. A ReplaceAction value that specifies the action to be taken for the current match.

Example:

Replaces text specified with regular expression with HTML.
public void replaceWithInsertHtml() throws Exception
{
    // Open the document.
    Document doc = new Document(getMyDir() + "Range.ReplaceWithInsertHtml.doc");

    doc.getRange().replace(Pattern.compile("<CustomerName>"), new ReplaceWithHtmlEvaluator(), false);

    // Save the modified document.
    doc.save(getMyDir() + "Range.ReplaceWithInsertHtml Out.doc");

}

private class ReplaceWithHtmlEvaluator implements IReplacingCallback
{
    /**
     * NOTE: This is a simplistic method that will only work well when the match
     * starts at the beginning of a run.
     */
    public int replacing(ReplacingArgs e) throws Exception
    {
        DocumentBuilder builder = new DocumentBuilder((Document)e.getMatchNode().getDocument());
        builder.moveTo(e.getMatchNode());
        // Replace '<CustomerName>' text with a red bold name.
        builder.insertHtml("<b><font color='red'>James Bond</font></b>");

        e.setReplacement("");
        return ReplaceAction.REPLACE;
    }
}

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() + "InsertDocument1.doc");
    mainDoc.getRange().replace(Pattern.compile("\\[MY_DOCUMENT\\]"), new InsertDocumentAtReplaceHandler(), false);
    mainDoc.save(getMyDir() + "InsertDocumentAtReplace Out.doc");
}

private class InsertDocumentAtReplaceHandler implements IReplacingCallback
{
    public int replacing(ReplacingArgs e) throws Exception
    {
        Document subDoc = new Document(getMyDir() + "InsertDocument2.doc");

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

        // Remove the paragraph with the match text.
        para.remove();

        return ReplaceAction.SKIP;
    }
}

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.