How can I hyperlink existing nodes in a document?

How can I hyperlink existing text in a document继续讨论:

HI team,
I found the thread and wanted to learn the topic, but i counld’nt get the attachemnts in the thread and maybe the demo in the thread was obsolete.

Would you like to make a demo with the last Aspose.Words.Java for me? I want to hyperlink exiting nodes(run/paragraph/list) in a document.

@Doraemon There are no attachments in the mentioned thread. If it is required to make some particular nodes hyperlinks, you should put these nodes into the hyperlink field value (between field separator and end). Please see our documentation to learn more about fields:
https://docs.aspose.com/words/java/introduction-to-fields/

For example, you can insert hyperlink with empty value and put the required nodes between separator and end. For example the following code wraps paragraph into hyperlink:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Get the paragraph, which should be hyperlinked.
Paragraph para = doc.getFirstSection().getBody().getFirstParagraph();
Node[] paraNodes = para.getChildNodes(NodeType.ANY, false).toArray();

// Move DocumentBuilder to the paragraph and insert hyperlink.
builder.moveTo(para);
Field hl = builder.insertHyperlink("", "https://www.aspose.com", false);
// Put all nodes from the target paragraph into the hyperlink value.
for (Node n : paraNodes)
    hl.getEnd().getParentNode().insertBefore(n, hl.getEnd());

doc.save("C:\\Temp\\out.docx");

in.docx (12.5 KB)
out.docx (10.1 KB)

@alexey.noskov
Thanks for your reply.

That’s what i need.

1 Like