Find and print the specific word


#1

Hi Team,

I want to find and print the number of occurrences of specific string in word document using aspose.words in java.

I want to search the word “FIGURE” in whole document and print the number of occurrences.

please kindly help me to solve the same issue.

Thanks & Regards,
priyanga G


#2

@priyanga,

Thanks for your inquiry. Please use the following code example to get the desired output.

Document doc = new Document(MyDir + "in.docx");
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
FindFigureCaption callback = new FindFigureCaption();
findReplaceOptions.setReplacingCallback(callback);
doc.getRange().replace("Figure", "", findReplaceOptions);

System.out.println(callback.mMatchNumber);

class FindFigureCaption implements IReplacingCallback {
    public int mMatchNumber;
    public int replacing(ReplacingArgs e) throws Exception {
        mMatchNumber++;
        return ReplaceAction.SKIP;
    }
}

#3

Hi @tahir.manzoor,

Thanks a lot.

Its working fine.

Thanks &regards,
Priyanga G


#4

@priyanga,

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.