We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace a word with hyperlink in java

Hi, Is it possible to replace a text with a hyperlink in aspose word v21.
When I was using v18 i was able to do it using ReplaceWithHyperlinkEvaluator in range.replace. After upgrade to v21, it’s not working anymore.
I user the answer in the following question to solve this issue when i was using v18

@ravindueranga,

You can build logic on the following Java code to be able to replace text in Word document with Hyperlink field:

Document doc = new Document("C:\\temp\\input.docx");

ReplacingCallbackHandler handler = new ReplacingCallbackHandler();
FindReplaceOptions opts = new FindReplaceOptions(FindReplaceDirection.BACKWARD);
opts.setReplacingCallback(handler);

doc.getRange().replace("Aspose", "https://www.aspose.com/", opts);

doc.save("C:\\Temp\\awjava-21.9.docx");

static class ReplacingCallbackHandler implements IReplacingCallback {

        public int replacing(ReplacingArgs e) throws Exception {
            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 removing.
            ArrayList runs = new ArrayList();

            // Find all runs that contain parts of the match string.
            int remainingLength = e.getMatch().group(0).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);
            }

            DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
            builder.moveTo((Run) runs.get(0));
            builder.getFont().setStyleIdentifier(StyleIdentifier.HYPERLINK);
            builder.insertHyperlink(e.getMatch().group(0), e.getReplacement(), false);

            //Now remove all runs in the sequence.
            for (Run run : (Iterable<Run>) runs)
                run.remove();

            return ReplaceAction.SKIP;
        }
    }

    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, position));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;
    }