Finding Hyperlinks in a word document in C#

2014-06-26 06:42:35 Written by  support iceblue
Rate this item
(0 votes)

Hyperlinks can point to files, emails, websites, imagers or video when readers click on it. Hyperlinks are widely used in word document for it is very convenient to direct readers to related, useful content. By using Spire.Doc, developers can add hyperlinks, finding hyperlinks and modify hyperlinks. This article will show you how to find all the existing hyperlinks in a word document in C#.

Download and install Spire.Doc for .NET and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how to finding hyperlinks in C#.

Firstly, view the word document which contains many hyperlinks:

Finding Hyperlinks in a word document

Please check the code of how to find all the hyperlinks in word document:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Collections.Generic;
using System.Drawing;
namespace FindHyperlink
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Spire.docx");
            List hyperlinks = new List();
            foreach (Section section in doc.Sections)
            {
                foreach (DocumentObject sec in section.Body.ChildObjects)
                {
                    if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
                        {
                            if (para.DocumentObjectType == DocumentObjectType.Field)
                            {
                                Field field = para as Field;

                                if (field.Type == FieldType.FieldHyperlink)
                                {
                                    hyperlinks.Add(field);
                                }

                            }
                        }
                    }
                }
            }
        }

The effective screenshot of the finding hyperlinks:

Finding Hyperlinks in a word document

Additional Info

  • tutorial_title: Finding Hyperlinks in a word in C#
Last modified on Friday, 03 September 2021 03:49