How to Insert Hyperlink in Word Document with C#/VB.NET

MS Word provides users with function to insert hyperlink. Hyperlink can lead readers to click it to learn more information which is related to contents. It can be website address, other Word document or Email address.

Generally speaking, we can insert hyperlink directly in document or for some special words, such as a famous person’s name, company name and so on. In this post, I will show how to insert hyperlink in Word by using C#/VB.NET.

In my example, I will show two hyperlinks in document, which is inserted directly. Type of two hyperlinks are website url.

Detailed Step Shown as Following:

  1. Create a new document and add section in it.
  2. Add paragraph in section and append text as title.
  3. Define a style, including font style, size and text color.
  4. Insert hyperlink. Firstly, append text as cue words. Secondly, apply style in cue word paragraph. Thirdly, append hyperlink in new paragraph. There are three parameters passed to AppendHyperlink method, link, link text and link type. Fourthly, apply style for hyperlink.
  5. Save and launch file.

Main Coding:

C#

using System;

using System.Drawing;

using Spire.Doc;

using Spire.Doc.Documents;

namespace Wordlink

{

    class Link

    {

        static void Main(string[] args)

        {

            //Create Document

            Document document = new Document();

            //Add Section and Paragraph

            Section section = document.AddSection();

            Paragraph paragraph = section.AddParagraph();

            //Add Text

            paragraph.AppendText(“Word Hyperlink”);

            paragraph.ApplyStyle(BuiltinStyle.Title);

            //Style

            ParagraphStyle style = new ParagraphStyle(document);

            style.Name = “Link Style”;

            style.CharacterFormat.FontName = “Calibri”;

            style.CharacterFormat.FontSize = 14;

            style.CharacterFormat.TextColor = Color.DarkOliveGreen;

            document.Styles.Add(style);

            //Insert Link

            paragraph = section.AddParagraph();

            paragraph.AppendText(“Visit WordPress Blog:”);

            paragraph.ApplyStyle(style.Name);

            paragraph = section.AddParagraph();

            paragraph.AppendHyperlink(“janewdaisy.wordpress.com”, “janewdaisy.wordpress.com”, HyperlinkType.WebLink);

            paragraph.ApplyStyle(style.Name);

            paragraph = section.AddParagraph();

            paragraph.AppendText(“Help Center:”);

            paragraph.ApplyStyle(style.Name);

            paragraph = section.AddParagraph();

            paragraph.AppendHyperlink(“en.support.wordpress.com”, “en.support.wordpress.com”, HyperlinkType.WebLink);

            paragraph.ApplyStyle(style.Name);

            //Save

            document.SaveToFile(“Link.docx”, FileFormat.Docx);

            //Launch

            System.Diagnostics.Process.Start(“Link.docx”);

        }

    }

}

VB.NET

Imports System

Imports System.Drawing

Imports Spire.Doc

Imports Spire.Doc.Documents

Namespace Wordlink

    Friend Class Link

        Shared Sub Main(ByVal args() As String)

            ‘Create Document

            Dim document As New Document()

            ‘Add Section and Paragraph

            Dim section As Section = document.AddSection()

            Dim paragraph As Paragraph = section.AddParagraph()

            ‘Add Text

            paragraph.AppendText(“Word Hyperlink”)

            paragraph.ApplyStyle(BuiltinStyle.Title)

            ‘Style

            Dim style As New ParagraphStyle(document)

            style.Name = “Link Style”

            style.CharacterFormat.FontName = “Calibri”

            style.CharacterFormat.FontSize = 14

            style.CharacterFormat.TextColor = Color.DarkOliveGreen

            document.Styles.Add(style)

            ‘Insert Link

            paragraph = section.AddParagraph()

            paragraph.AppendText(“Visit WordPress Blog:”)

            paragraph.ApplyStyle(style.Name)

            paragraph = section.AddParagraph()

            paragraph.AppendHyperlink(“janewdaisy.wordpress.com”, “janewdaisy.wordpress.com”, HyperlinkType.WebLink)

            paragraph.ApplyStyle(style.Name)

            paragraph = section.AddParagraph()

            paragraph.AppendText(“Help Center:”)

            paragraph.ApplyStyle(style.Name)

            paragraph = section.AddParagraph()

            paragraph.AppendHyperlink(“en.support.wordpress.com”, “en.support.wordpress.com”, HyperlinkType.WebLink)

            paragraph.ApplyStyle(style.Name)

            ‘Save

            document.SaveToFile(“Link.docx”, FileFormat.Docx)

            ‘Launch

            System.Diagnostics.Process.Start(“Link.docx”)

        End Sub

    End Class

End Namespace

Result Shown as Following:

____________________________________________________________________________________________

Click Here to Learn more about Spire.Doc

Click Here to Download Spire.Doc

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s