Search text & apply bookmark in exiting word document

In the exiting word document, I want to search the text/value & apply/insert the bookmark on the same, In can be anywhere in the word document

Hi

Thanks for your request. You should use ReplaceEvaluator in this case. Please try using the following code:

// Create regex
Pattern regex = Pattern.compile("word");
// Open document
Document doc = new Document("C:\\Temp\\in.doc");
// find placeholders
doc.getRange().replace(regex, new ReplaceEvaluatorInsertBookmark(), false);
// Save document
doc.save("C:\\Temp\\out.doc");

=====================================================================

public class ReplaceEvaluatorInsertBookmark implements ReplaceEvaluator
{
    // There could be many matched words,   
    // we will use counter to generate unique names for bookmarks
    private int mBookmarkCounter = 0;
    public synchronized int replace(Object object, ReplaceEvaluatorArgs e) throws Exception
    {
        // Get MatchNode
        Run matchedRun = (Run)e.getMatchNode();
        // Get word
        String word = e.getMatch().group();
        // The run can contain whole mathed word or part of the word
        // When run contains only part of the word IndexOf will return negative value
        // Here we search for word start index
        int index = matchedRun.getText().lastIndexOf(word);
        int wordLength = word.length();
        while(index < 0)
            index = matchedRun.getText().lastIndexOf(word.substring(0, wordLength--));
        // matched run can contain text before matched word, so me should split run here
        if(index > 0)
        {
            Run beforeRun = (Run)matchedRun.deepClone(true);
            beforeRun.setText(matchedRun.getText().substring(0, index));
            matchedRun.setText(matchedRun.getText().substring(index));
            matchedRun.getParentNode().insertBefore(beforeRun, matchedRun);
        }
        // Create documentbuilder to insert bookmark
        DocumentBuilder builder = new DocumentBuilder(e.getMatchNode().getDocument());
        // Move builser to start node
        builder.moveTo(matchedRun);
        // Insert bookmark
        builder.startBookmark("bk" + mBookmarkCounter);
        builder.endBookmark("bk" + mBookmarkCounter);
        // increase counter
        mBookmarkCounter++;
        return ReplaceAction.SKIP;
    }
}

Also see the following link to learn more about ReplaceEvaluator.
https://reference.aspose.com/words/java/com.aspose.words/range#replace(java.util.regex.Pattern,java.lang.String,com.aspose.words.FindReplaceOptions)
Hope this helps.
Best regards.

Does document.Range include the Header and Footer text, or does this require a separate search?
Thanks,
Jeff

Hi Jeff,
Thanks for your inquiry.
Yes it does, the Range object is meant to provide a “flat view” of the document. In this case it is the range of the entire document so headers and footers will be searched too.
In some cases if you only want to search the body or only the headers and footers you can add some code to check if the match node has an ancestor of Body or of HeaderFooter.
If you have any further queries, please feel free to ask.
Thanks,