How to add a hyperlink into a document using C# and .NET

  1. Add SautinSoft.Document from Nuget.
  2. Create a new document.
  3. Create a hyperlink.
  4. Insert the hyperlink at the document start.

Complete code

using SautinSoft.Document;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free 100-day key here:   
            // https://sautinsoft.com/start-for-free/

            AddHyperlink();
        }
        
		/// <summary>
        /// How to add a hyperlink into a document. 
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/hyperlinks.php
        /// </remarks>		
        public static void AddHyperlink()
        {
            string docxPath = @"Hyperlink.docx";

            // Let's create a simple DOCX document with a hyperlink.
            DocumentCore dc = new DocumentCore();

            Hyperlink hpl = new Hyperlink(dc, "http://www.zoo.org", "Welcome to Zoo!");
            (hpl.DisplayInlines[0] as Run).CharacterFormat = new CharacterFormat() { Size = 16, FontColor = new Color("#358CCB"), UnderlineStyle = UnderlineType.Single };
            hpl.ScreenTip = "Welcome to WoodLand Zoo!";

            Paragraph p = new Paragraph(dc);
            p.Inlines.Add(hpl);
            p.ParagraphFormat.Alignment = HorizontalAlignment.Center;

            dc.Content.Start.Insert(p.Content);

            // Save our document to DOCX format.
            dc.Save(docxPath);

           // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docxPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        AddHyperlink()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' How to add a hyperlink into a document. 
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/hyperlinks.php
    ''' </remarks>		
    Sub AddHyperlink()
        Dim docxPath As String = "Hyperlink.docx"

        ' Let's create a simple DOCX document with a hyperlink.
        Dim dc As New DocumentCore()

        Dim hpl As New Hyperlink(dc, "http://www.zoo.org", "Welcome to Zoo!")
        TryCast(hpl.DisplayInlines(0), Run).CharacterFormat = New CharacterFormat() With {
            .Size = 16,
            .FontColor = New Color("#358CCB"),
            .UnderlineStyle = UnderlineType.Single
        }
        hpl.ScreenTip = "Welcome to WoodLand Zoo!"

        Dim p As New Paragraph(dc)
        p.Inlines.Add(hpl)
        p.ParagraphFormat.Alignment = HorizontalAlignment.Center

        dc.Content.Start.Insert(p.Content)

        ' Save our document to DOCX format.
        dc.Save(docxPath)

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(docxPath) With {.UseShellExecute = True})
    End Sub
End Module

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.