Get The Of Hyperlinks in Word Document

Hi,

I need to know what is the optimum way to get a list of Hyperlinks inside a word Document (using Aspose.Words).

Thank you,

Hi Jawad,

Thanks for your inquiry.

A hyperlink is represented by a HYPERLINK field in a Word document. A field in Aspose.Words consists of several nodes and it might be difficult to work with all those nodes directly. However, you can loop through all HYPERLINK fields in your document by using the following code snippet:

NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);
foreach(FieldStart fieldStart in fieldStarts)
{
    if (fieldStart.FieldType.Equals(FieldType.FieldHyperlink))
    {
        /* Do something here */
    }
}

I hope, this helps.

Best regards,

Thank you. That’s helpful.
Though I need to know also how to get the name of the Hyperlink (like #MyBookmark). Thanks,

Hi Jawad,

Thanks for your inquiry. I have attached a “facade” class that makes it easier to work with a hyperlink field in a Word document. You can use the following code to retrieve link name:

NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);
foreach(FieldStart fieldStart in fieldStarts)
{
    if (fieldStart.FieldType.Equals(FieldType.FieldHyperlink))
    {
        Hyperlink hyperlink = new Hyperlink(fieldStart);
        string linkName = hyperlink.Name;
    }
}

I hope, this helps.

Best regards,