Free Support Forum - aspose.com

How to modify hyperlink in document?


#1

Hi, support,

I have a question that how to modify hyperlink in document. That is to say, for example, [This is a demo for modifying hyperlink], who has been added a hyperlink, its hyperlink address is “http://www.google.com”, its ScreenTip is “Click it to access the site”, and its TextToDisplay is “This is a demo for modifying hyperlink”. Now, I want to modify it and the modified hyperlink is [This is a demo for modified hyperlink],its hyperlink address is “http://www.aspose.com”, its ScreenTip is “Click it to access the Aspose.com site”, and its TextToDisplay is “This is a demo for modified hyperlink”.
How to do it by Aspose.Words.dll and VB.net?

Thanks in advance!


#2

@ducaisoft

Thanks for your inquiry. FieldHyperlink class implements the HYPERLINK field. You can use FieldHyperlink.Address, FieldHyperlink.ScreenTip, FieldHyperlink.Result properties to achieve your requirement.

Please check the following code example. We have attached the input and output documents with this post for your kind reference. Docs.zip (18.3 KB)

Dim doc As Document = New Document(MyDir & "in.docx")
Dim fieldHyperlink As FieldHyperlink = CType(doc.Range.Fields(0), FieldHyperlink)
fieldHyperlink.Result = "This is a demo for modified hyperlink"
fieldHyperlink.Address = "http://www.aspose.com/"
fieldHyperlink.ScreenTip = "Click it to access the Aspose.com site"
doc.Save(MyDir & "19.2.docx")

#3

Thanks for your reply!
Unfortunately, I found a bug for Aspose.Words.Dll Version 19.2. That is to say, the hyperlink cannot be removed if it is anchored to Inline or floating image. Hope to fix and update this feature.


    Doc.UnlinkFields()
    For Each field As Global.Aspose.Words.Fields.Field In Doc.Range.Fields
        field.Unlink()
    Next

This code can not remove hyperlink whose anchor is inline or floating image for version 19.2.


#4

@ducaisoft,

To remove/modify hyperlink of an image, please use Shape.HRef property.

Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
shape.HRef = ""; // removes the hyperlink 
shape.HRef = "https://www.aspose.com"; // modifies hyperlink

Hope, this helps.