Free Support Forum - aspose.com

How to traverse each hyperlink


#1

Hi, support,

I have a question that how to traverse each hyperlink in a document for editing,clearing or modifying by aspose.words.dll and VB.net?
It seem the Hyerlink node does not exist in the nodecollections.
Thanks for your help!


#2

@ducaisoft,

The following code should help:

foreach (Field field in doc.Range.Fields)
{
    if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink))
    {
        FieldHyperlink hyperlink = (FieldHyperlink)field;
        // more code
    }
}

#3

Thank for your suggestion.
But I still fail.
My purpose is to traverse each Hyperlink element, and then determine who is its anchor (pure text, image, textbox, autoshape, smartshape,artword,equation,OleObj,media,insertFileObj, and so on), and then determine whether the hyperlink need modify/delete.

That is to say, the hyperlink will be modified/deleted if the hyperlink is anchored to pure text or picture. The modification may be replaced the hyperlink with a new one; the deleting maybe directly remove the hyperlink as well as its anchor, or only unlink the hyperlink and keep the text/picture left.

How to work it out?

How to work it out?


#4

@ducaisoft,

To un-link hyperlink from plain text, please use FieldHyperlink.Unlink() method and 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

Please ZIP and upload your sample Word document containing all of these hyperlinked elements and attach/upload here for testing. We will investigate the scenario on our end and provide you more information.