How to Find & Highlight a Particular Word or Phrase in a Document inside Android
This article describes now to programmatically find and highlight a particular word or a phrase in a document using Aspose.Words. It might seem easy at first to just find the string of text in a document and change its formatting, but the main difficulty is that due to formatting, the match string could be spread over several runs of text. Consider the following example. The phrase “Hello World!” consists of three different runs, its beginning is italic, middle is bold, while the last part – regular text. In addition to formatting, any bookmarks in the middle of text will split it into more runs. This article provides a solution designed to handle the described case – if necessary it collects the word (or phrase) from several runs, while skipping non-run nodes. The sample code opens a document and find any instance of the text “your document”. A replace handler is set up to handle the logic to be applied to each resulting match found. In this case the resulting runs are split around the txt and the resulting runs highlighted, even those matches that have different formatting and span across multiple runs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//your code here...
package FindAndHighlight;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.awt.Color;
import java.io.File;
import java.net.URI;
import com.aspose.words.Document;
import com.aspose.words.IReplacingCallback;
import com.aspose.words.ReplaceAction;
import com.aspose.words.NodeType;
import com.aspose.words.ReplacingArgs;
import com.aspose.words.Node;
import com.aspose.words.Run;
class Program
{
public static void main(String[] args) throws Exception
{
// Sample infrastructure.
URI exeDir = Program.class.getResource("").toURI();
String dataDir = new File(exeDir.resolve("../../Data")) + File.separator;
Document doc = new Document(dataDir + "TestFile.doc");
// We want the "your document" phrase to be highlighted.
Pattern regex = Pattern.compile("your document", Pattern.CASE_INSENSITIVE);
// Generally it is recommend if you are modifying the document in a custom replacement evaluator
// then you should use backward replacement by specifying false value to the third parameter of the replace method.
doc.getRange().replace(regex, new ReplaceEvaluatorFindAndHighlight(), false);
// Save the output document.
doc.save(dataDir + "TestFile Out.doc");
}
}
class ReplaceEvaluatorFindAndHighlight implements IReplacingCallback
{
/**
* This method is called by the Aspose.Words find and replace engine for each match.
* This method highlights the match string, even if it spans multiple runs.
*/
public int replacing(ReplacingArgs e) throws Exception
{
// This is a Run node that contains either the beginning or the complete match.
X
Url: http://www.aspose.com/android/word-component.aspx
Language: Java | User: Sheraz Khan | Created: Oct 21, 2015 | Tags: Search text in MS Word in Android highlight a particular word Search a phrase in Word Docs Word Android API How to Find & Highlight Text in Android Aspose.Words for Android