Replacing a text with Hyperlink in MS WORD doc using JAVA

Hello,

  1. I would like know if Aspose can search and replace a text in a MS word doc file with a Hyperlink?
    If yes, can someone give me an example of it?
    Thanks,
1 Like

Hi
Thanks for your request. You can achieve this using ReplaceEvaluator. See the following code for example:

public static void main(String[] args) throws Exception {
    // Open the document.
    Document doc = new Document("C:\\Temp\\in.doc");
    doc.getRange().replace(Pattern.compile("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"),
            new ReplaceWithHyperlinkEvaluator(), false);
    // Save the modified document.
    doc.save("C:\\Temp\\out.doc");
}
public class ReplaceWithHyperlinkEvaluator implements ReplaceEvaluator
{
    /**
        * A user implemented ReplaceEvaluator.replace() method is called for each
        * match found during a replace operation.
        *
        * @return An enumerated value that specifies the action to be taken for the current match.
        */
    public int replace(Object sender, ReplaceEvaluatorArgs e) throws Exception
    {
        DocumentBuilder builder = new DocumentBuilder(e.getMatchNode().getDocument());
        // Move to mathed node
        builder.moveTo(e.getMatchNode());
        builder.getFont().setUnderline(Underline.SINGLE);
        builder.getFont().setColor(Color.BLUE);
        // Insert hyperlink
        builder.insertHyperlink(e.getMatch().group(), e.getMatch().group(), false);
        e.setReplacement("");
        return ReplaceAction.REPLACE;
    }
}

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.

Alexey, Thanks for the response. I tried the code given by you. It seems to work partially, what I mean by partially is that new URL that is written is not at the same position/location as the replaced text.
For example: If I try to replace “Java” with a Hyperlink in the following line of the document, the replaced Hyperlink appears at the begining of the line instead of being at the third word position.
Input:
Sun Certified Java Programmer
Ouput of Replacement
NEW_URL Sun Certified Programmer.
Please let me know if some code tweaking needs to be done.
Thanks,
Santosh

Hi
Thanks for your request. I think the following code will work for you.

public static void main(String[] args) throws Exception {
    // Open the document.
    Document doc = new Document("C:\\Temp\\in.doc");
    doc.getRange().replace(Pattern.compile("aspose"),
            new ReplaceWithHyperlinkEvaluator(), false);
    // Save the modified document.
    doc.save("C:\\Temp\\out.doc");
}
public class ReplaceWithHyperlinkEvaluator implements ReplaceEvaluator
{
    /**
        * A user implemented ReplaceEvaluator.replace() method is called for each
        * match found during a replace operation.
        *
        * @return An enumerated value that specifies the action to be taken for the current match.
        */
    public int replace(Object sender, ReplaceEvaluatorArgs e) throws Exception
    {
        // Get MatchNode
        Run run1 = (Run)e.getMatchNode();
        // Create Run
        Run run2 = (Run)run1.deepClone(true);
        // Get index of match value
        int index = run1.getText().indexOf(e.getMatch().group());
        // split run that contains matched text
        run2.setText(run1.getText().substring(index + e.getMatch().group().length()));
        run1.setText(run1.getText().substring(0, index));
        run1.getParentParagraph().insertAfter(run2, run1);
        DocumentBuilder builder = new DocumentBuilder(e.getMatchNode().getDocument());
        // Move to mathed node
        builder.moveTo(run2);
        builder.getFont().setUnderline(Underline.SINGLE);
        builder.getFont().setColor(Color.BLUE);
        // Insert hyperlink
        builder.insertHyperlink(e.getMatch().group(), "http://www.aspose.com", false);
        return ReplaceAction.SKIP;
    }
}

Hope this helps.
Best regards.

This code worked. Will do more testing and get back to you if something breaks.
Thanks,

Hi Alexey,
I am doing the similar stuff but I don’t want to replace the text with hyperlink.
My Problem is that I have to search & replace text with some other text.
For example, my doc has a line…
Illustration :
I have to replace with ABC12.
Can you please tell me how can I do it.
Thanks in anticipation
Bhoomica

Hi
Thanks for your request. I answered this question here:
https://forum.aspose.com/t/98645
Best regards.

A post was split to a new topic: How to insert hyperlink for the StructuredDocumentTag