Hyperlinks in .NET Word

13 Aug 202619 minutes to read

Hyperlinks have two parts: the address and the display content. The Syncfusion® .NET Word (DocIO) library supports the following hyperlink types:

  • Web hyperlink
  • Email hyperlink
  • File hyperlink
  • Bookmark hyperlink
  • Image hyperlink

The following code example shows how to insert a web link.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("Web hyperlink:  ");
paragraph = section.AddParagraph();
//Appends a web hyperlink to the paragraph
paragraph.AppendHyperlink("http://www.syncfusion.com", "Syncfusion", HyperlinkType.WebLink);
//Saves the Word document to a MemoryStream
using (MemoryStream stream = new MemoryStream())
{
    document.Save(stream, FormatType.Docx);
}
//Closes the Word document
document.Close();
//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("Web hyperlink:  ");
paragraph = section.AddParagraph();
//Appends a web hyperlink to the paragraph
paragraph.AppendHyperlink("http://www.syncfusion.com", "Syncfusion", HyperlinkType.WebLink);
//Saves the Word document
document.Save("Sample.docx", FormatType.Docx);
//Closes the document
document.Close();
'Creates a new Word document
Dim document As New WordDocument()
'Adds new section to the document
Dim section As IWSection = document.AddSection()
'Adds new paragraph to the section
Dim paragraph As IWParagraph = section.AddParagraph()
paragraph.AppendText("Web hyperlink:  ")
paragraph = section.AddParagraph()
'Appends a web hyperlink to the paragraph
paragraph.AppendHyperlink("http://www.syncfusion.com", "Syncfusion", HyperlinkType.WebLink)
'Saves the Word document
document.Save("Sample.docx", FormatType.Docx)
'Closes the document
document.Close()

You can download a complete working sample from GitHub.

The following code example shows how to add an email link.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("Email hyperlink: ");
paragraph = section.AddParagraph();
//Appends an email hyperlink to the paragraph
paragraph.AppendHyperlink("mailto:[email protected]", "Sales", HyperlinkType.EMailLink);
//Saves the Word document to a MemoryStream
using (MemoryStream stream = new MemoryStream())
{
    document.Save(stream, FormatType.Docx);
}
//Closes the Word document
document.Close();
//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("Email hyperlink: ");
paragraph = section.AddParagraph();
//Appends an email hyperlink to the paragraph
paragraph.AppendHyperlink("mailto:[email protected]", "Sales", HyperlinkType.EMailLink);
//Saves the Word document
document.Save("Sample.docx", FormatType.Docx);
//Closes the document
document.Close();
'Creates a new Word document
Dim document As New WordDocument()
'Adds new section to the document
Dim section As IWSection = document.AddSection()
'Adds new paragraph to the section
Dim paragraph As IWParagraph = section.AddParagraph()
paragraph.AppendText("Email hyperlink: ")
paragraph = section.AddParagraph()
'Appends an email hyperlink to the paragraph
paragraph.AppendHyperlink("mailto:[email protected]", "Sales", HyperlinkType.EMailLink)
'Saves the Word document
document.Save("Sample.docx", FormatType.Docx)
'Closes the document
document.Close()

You can download a complete working sample from GitHub.

The following code example shows how to add a file hyperlink.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("File hyperlink: ");
paragraph = section.AddParagraph();
//Appends a file hyperlink to the paragraph
paragraph.AppendHyperlink(@"Template.docx", "File", HyperlinkType.FileLink);
//Saves the Word document to a MemoryStream
using (MemoryStream stream = new MemoryStream())
{
    document.Save(stream, FormatType.Docx);
}
//Closes the Word document
document.Close();
//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("File hyperlink: ");
paragraph = section.AddParagraph();
//Appends a file hyperlink to the paragraph
paragraph.AppendHyperlink(@"Template.docx", "File", HyperlinkType.FileLink);
//Saves the Word document
document.Save("Sample.docx", FormatType.Docx);
//Closes the document
document.Close();
'Creates a new Word document
Dim document As New WordDocument()
'Adds new section to the document
Dim section As IWSection = document.AddSection()
'Adds new paragraph to the section
Dim paragraph As IWParagraph = section.AddParagraph()
paragraph.AppendText("File hyperlink: ")
paragraph = section.AddParagraph()
'Appends a file hyperlink to the paragraph
paragraph.AppendHyperlink("Template.docx", "File", HyperlinkType.FileLink)
'Saves the Word document
document.Save("Sample.docx", FormatType.Docx)
'Closes the document
document.Close()

You can download a complete working sample from GitHub.

The following code example explains how to add a bookmark hyperlink.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
//Creates a new bookmark
paragraph.AppendBookmarkStart("Introduction");
paragraph.AppendText("Hyperlink");
paragraph.AppendBookmarkEnd("Introduction");
paragraph.AppendText("\nA hyperlink is a reference or navigation element in a document to another section of the same document or to another document that may be on or part of a (different) domain.");
paragraph = section.AddParagraph();
paragraph.AppendText("Bookmark hyperlink: ");
paragraph = section.AddParagraph();
//Appends a bookmark hyperlink to the paragraph
paragraph.AppendHyperlink("Introduction", "Bookmark", HyperlinkType.Bookmark);
//Saves the Word document to a MemoryStream
using (MemoryStream stream = new MemoryStream())
{
    document.Save(stream, FormatType.Docx);
}
//Closes the Word document
document.Close();
//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
//Creates a new bookmark
paragraph.AppendBookmarkStart("Introduction");
paragraph.AppendText("Hyperlink");
paragraph.AppendBookmarkEnd("Introduction");
paragraph.AppendText("\nA hyperlink is a reference or navigation element in a document to another section of the same document or to another document that may be on or part of a (different) domain.");
paragraph = section.AddParagraph();
paragraph.AppendText("Bookmark hyperlink: ");
paragraph = section.AddParagraph();
//Appends a bookmark hyperlink to the paragraph
paragraph.AppendHyperlink("Introduction", "Bookmark", HyperlinkType.Bookmark);
//Saves the Word document
document.Save("Sample.docx", FormatType.Docx);
//Closes the document
document.Close();
'Creates a new Word document
Dim document As New WordDocument()
'Adds new section to the document
Dim section As IWSection = document.AddSection()
'Adds new paragraph to the section
Dim paragraph As IWParagraph = section.AddParagraph()
'Creates a new bookmark
paragraph.AppendBookmarkStart("Introduction")
paragraph.AppendText("Hyperlink")
paragraph.AppendBookmarkEnd("Introduction")
paragraph.AppendText(vbLf & "A hyperlink is a reference or navigation element in a document to another section of the same document or to another document that may be on or part of a (different) domain.")
paragraph = section.AddParagraph()
paragraph.AppendText("Bookmark hyperlink: ")
paragraph = section.AddParagraph()
'Appends a bookmark hyperlink to the paragraph
paragraph.AppendHyperlink("Introduction", "Bookmark", HyperlinkType.Bookmark)
'Saves the Word document
document.Save("Sample.docx", FormatType.Docx)
'Closes the document
document.Close()

You can download a complete working sample from GitHub.

The display content for a hyperlink can also be an image that redirects to other content.

The following code example explains how to add an image hyperlink.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("Image hyperlink: ");
paragraph = section.AddParagraph();
//Creates a new image instance and loads the image
WPicture picture = new WPicture(document);
using (FileStream imageStream = new FileStream(@"Mountain-200.jpg", FileMode.Open, FileAccess.ReadWrite))
{
    picture.LoadImage(imageStream);
}
//Appends an image hyperlink to the paragraph
paragraph.AppendHyperlink("http://www.syncfusion.com", picture, HyperlinkType.WebLink);
//Saves the Word document to a MemoryStream
using (MemoryStream stream = new MemoryStream())
{
    document.Save(stream, FormatType.Docx);
}
//Closes the Word document
document.Close();
//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("Image hyperlink: ");
paragraph = section.AddParagraph();
//Creates a new image instance and loads the image
WPicture picture = new WPicture(document);
picture.LoadImage(Image.FromFile("Image.png"));
//Appends an image hyperlink to the paragraph
paragraph.AppendHyperlink("http://www.syncfusion.com", picture, HyperlinkType.WebLink);
//Saves the Word document
document.Save("Sample.docx", FormatType.Docx);
//Closes the document
document.Close();
'Creates a new Word document
Dim document As New WordDocument()
'Adds new section to the document
Dim section As IWSection = document.AddSection()
'Adds new paragraph to the section
Dim paragraph As IWParagraph = section.AddParagraph()
paragraph.AppendText("Image hyperlink: ")
paragraph = section.AddParagraph()
'Creates a new image instance and loads the image
Dim picture As New WPicture(document)
picture.LoadImage(Image.FromFile("Image.png"))
'Appends an image hyperlink to the paragraph
paragraph.AppendHyperlink("http://www.syncfusion.com", picture, HyperlinkType.WebLink)
'Saves the Word document
document.Save("Sample.docx", FormatType.Docx)
'Closes the document
document.Close()

You can download a complete working sample from GitHub.

The following code example explains how to modify the URL of an existing hyperlink.

using (FileStream fileStream = new FileStream(@"Sample.docx", FileMode.Open, FileAccess.ReadWrite))
{
    //Loads the template document
    WordDocument document = new WordDocument(fileStream, FormatType.Docx);
    WParagraph paragraph = document.LastParagraph;
    //Iterates through the paragraph items
    foreach (ParagraphItem item in paragraph.ChildEntities)
    {
        if (item is WField)
        {
            if ((item as WField).FieldType == FieldType.FieldHyperlink)
            {
                //Gets the hyperlink field
                Hyperlink link = new Hyperlink(item as WField);
                if (link.Type == HyperlinkType.WebLink)
                {
                    //Modifies the URL of the hyperlink
                    link.Uri = "http://www.google.com";
                    link.TextToDisplay = "Google";
                    break;
                }
            }
        }
    }
    //Saves the Word document to a MemoryStream
    using (MemoryStream stream = new MemoryStream())
    {
        document.Save(stream, FormatType.Docx);
    }
    //Closes the Word document
    document.Close();
}
//Loads the template document
WordDocument document = new WordDocument("Sample.docx", FormatType.Docx);
WParagraph paragraph = document.LastParagraph;
//Iterates through the paragraph items
foreach (ParagraphItem item in paragraph.ChildEntities)
{
    if (item is WField)
    {
        if ((item as WField).FieldType == FieldType.FieldHyperlink)
        {
            //Gets the hyperlink field
            Hyperlink link = new Hyperlink(item as WField);
            if (link.Type == HyperlinkType.WebLink)
            {
                //Modifies the URL of the hyperlink
                link.Uri = "http://www.google.com";
                link.TextToDisplay = "Google";
                break;
            }
        }
    }
}
//Saves and closes the Word document
document.Save("Sample.docx", FormatType.Docx);
document.Close();
'Loads the template document
Dim document As New WordDocument("Sample.docx", FormatType.Docx)
Dim paragraph As WParagraph = document.LastParagraph
'Iterates through the paragraph items
For Each item As ParagraphItem In paragraph.ChildEntities
    If TypeOf item Is WField Then
        Dim field As WField = DirectCast(item, WField)
        If field.FieldType = FieldType.FieldHyperlink Then
            'Gets the hyperlink field
            Dim link As New Hyperlink(field)
            If link.Type = HyperlinkType.WebLink Then
                'Modifies the URL of the hyperlink
                link.Uri = "http://www.google.com"
                link.TextToDisplay = "Google"
                Exit For
            End If
        End If
    End If
Next
'Saves and closes the Word document
document.Save("Sample.docx", FormatType.Docx)
document.Close()

You can download a complete working sample from GitHub.

See Also

The following knowledge base articles cover scenarios not discussed above — removing hyperlinks, replacing text with hyperlinks, and stripping hyperlink styling.