Friday, 07 January 2011 07:10

Insert Hyperlink to Word in C#, VB.NET

Word Hyperlink can be inserted automatically when users press ENTER or SPACEBAR after typing address of an existing Web page, such as www.e-iceblue.com. Also, users can insert customized hyperlink in Word for text to picture that they want to display as hyperlink. The hyperlink for text or picture can be Web page, E-mail address, other files or document.

Spire.Doc for .NET, a professional .NET Word component to manipulate Word documents, enables users to insert hyperlink in Word with C#/VB.NET. You can invoke the method paragraph.AppendHyperlink(string link, string text, HyperlinkType) to insert hyperlink in Word directly via Spire.Doc for .NET. And the HyperlinkType in method can be set as WebLink, EmailLink etc. Below is an effective screenshot after inserting hyperlink to Word in C#, VB.NET

Insert Word Hyperlink

Download and install Spire.Doc for .NET and then use the following code to insert hyperlink in Word.

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace InsertHyperlink
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create Word
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Set Paragraph Styles
            ParagraphStyle txtStyle = new ParagraphStyle(document);
            txtStyle.Name = "Style";
            txtStyle.CharacterFormat.FontName = "Impact";
            txtStyle.CharacterFormat.FontSize = 16;
            txtStyle.CharacterFormat.TextColor = Color.RosyBrown;
            document.Styles.Add(txtStyle);

            ParagraphStyle hyperlinkstyle = new ParagraphStyle(document);
            hyperlinkstyle.Name = "linkStyle";
            hyperlinkstyle.CharacterFormat.FontName = "Calibri";
            hyperlinkstyle.CharacterFormat.FontSize = 15;
            document.Styles.Add(hyperlinkstyle);

            //Append Text and Set Text Format
            paragraph = section.AddParagraph();
            paragraph.AppendText("Home page");
            paragraph.ApplyStyle(txtStyle.Name);

            //Insert Hyperlink(Web Page)
            paragraph = section.AddParagraph();
            paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
            paragraph.ApplyStyle(hyperlinkstyle.Name);

            //Append text
            paragraph = section.AddParagraph();
            paragraph.AppendText("Contact US");
            paragraph.ApplyStyle(txtStyle.Name);

            //Insert Hyperlink(E-mail Address);
            paragraph = section.AddParagraph();
            paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink);
            paragraph.ApplyStyle(hyperlinkstyle.Name);

            //Save and Launch
            document.SaveToFile("Hyperlink.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Hyperlink.docx");

        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Namespace InsertHyperlink
	Class Program
		Private Shared Sub Main(args As String())
			'Create Word
			Dim document As New Document()
			Dim section As Section = document.AddSection()
			Dim paragraph As Paragraph = section.AddParagraph()

			'Set Paragraph Styles
			Dim txtStyle As New ParagraphStyle(document)
			txtStyle.Name = "Style"
			txtStyle.CharacterFormat.FontName = "Impact"
			txtStyle.CharacterFormat.FontSize = 16
			txtStyle.CharacterFormat.TextColor = Color.RosyBrown
			document.Styles.Add(txtStyle)

			Dim hyperlinkstyle As New ParagraphStyle(document)
			hyperlinkstyle.Name = "linkStyle"
			hyperlinkstyle.CharacterFormat.FontName = "Calibri"
			hyperlinkstyle.CharacterFormat.FontSize = 15
			document.Styles.Add(hyperlinkstyle)

			'Append Text and Set Text Format
			paragraph = section.AddParagraph()
			paragraph.AppendText("Home page")
			paragraph.ApplyStyle(txtStyle.Name)

			'Insert Hyperlink(Web Page)
			paragraph = section.AddParagraph()
			paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink)
			paragraph.ApplyStyle(hyperlinkstyle.Name)

			'Append text
			paragraph = section.AddParagraph()
			paragraph.AppendText("Contact US")
			paragraph.ApplyStyle(txtStyle.Name)

			'Insert Hyperlink(E-mail Address);
			paragraph = section.AddParagraph()
			paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink)
			paragraph.ApplyStyle(hyperlinkstyle.Name)

			'Save and Launch
			document.SaveToFile("Hyperlink.docx", FileFormat.Docx)
			System.Diagnostics.Process.Start("Hyperlink.docx")

		End Sub
	End Class
End Namespace

Spire.Doc is a Microsoft Word component, which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF, .NET and Silverlight.

Published in Hyperlink