Hi,
I have tried this code for hyperlinking occurrences of a particular string but I am getting duplicate values of the string appended with the original string in the output document. for example : I want to hyperlink 800 f2d 111 in the document but if the input document is having 800 f2d 111, then m getting an output 800 f2d 111800 f2d 111f2d 800 f2d 111111 instead of 800 f2d 111 .Please help me understand how the code is working and why am I getting the particular output.The java code is :
public class FindAndReplace {
static Document doc=null;
public static Document getDoc() {
return doc;
}
public static void setDoc(Document doc) {
FindAndReplace.doc = doc;
}
public static void main(String[] args) throws Exception
{
// Sample infrastructure.
URI exeDir = FindAndReplace.class.getResource("").toURI();
//System.out.println(exeDir.toString());
File dataDir = new File(“C:\Users\LAKHOTRX\DCW\AsposePOC\src\TestDoc.docx”);
System.out.println(dataDir.getAbsolutePath());
doc = new Document(dataDir.getAbsolutePath());
// We want the “your document” phrase to be highlighted.
Pattern regex = Pattern.compile(“800 f2d 111”, 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 + “TestFileOut.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.
Node currentNode = e.getMatchNode();
// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.getMatchOffset() > 0)
currentNode = splitRun((Run)currentNode, e.getMatchOffset());
// This array is used to store all nodes of the match for further highlighting.
ArrayList runs = new ArrayList();
// Find all runs that contain parts of the match string.
int remainingLength = e.getMatch().group().length();
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.getText().length() <= remainingLength))
{
runs.add(currentNode);
remainingLength = remainingLength - currentNode.getText().length();
// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.getNextSibling();
}
while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
}
// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0))
{
splitRun((Run)currentNode, remainingLength);
runs.add(currentNode);
}
// // Move to mathed node
// Now highlight all runs in the sequence.
for (Run run : (Iterable) runs)
{ DocumentBuilder builder = new DocumentBuilder(FindAndReplace.getDoc());
// run.getFont().setHighlightColor(Color.YELLOW);
builder.moveTo(run);
builder.getFont().setUnderline(Underline.SINGLE);
builder.getFont().setColor(Color.BLUE);
builder.insertHyperlink(e.getMatch().group(), “http://www.google.com”,
false);
}
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.REPLACE;
}
/
* Splits text of the specified run into two runs.
* Inserts the new run just after the specified run.
*/
private static Run splitRun(Run run, int position) throws Exception
{
Run afterRun = (Run)run.deepClone(true);
afterRun.setText(run.getText().substring(position));
run.setText(run.getText().substring((0), (0) + (position)));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}
}
Regards,
Rajesh