How to create word hyperlinks in C# on WPF applications

Word hyperlink gives a large amount of information of the original word and image. The text and image with hyperlinks can guides readers to a Web page, e-mail address, or the other files and documents. In this article, we’re focusing on how to insert hyperlink into the text in C# on WPF applications with the help of Spire.Doc for WPF.

Note: Before Start, please download the latest version of Spire.Doc and add Spire.Doc.Wpf.dll in the bin folder as the reference of Visual Studio.

Here comes to the code snippets:

Step 1: Create a word document and add a section and paragraph to it.

Document document = new Document();
Section Sec = document.AddSection();
Paragraph Para = Sec.AddParagraph();

Step 2: Set the styles for the text and hyperlinks on the paragraph.

ParagraphStyle txtStyle = new ParagraphStyle(document);
txtStyle.Name = "Style";
txtStyle.CharacterFormat.FontName = "Calibri";
txtStyle.CharacterFormat.FontSize = 16;
txtStyle.CharacterFormat.TextColor = System.Drawing.Color.RosyBrown;
document.Styles.Add(txtStyle);
ParagraphStyle hyperlinkstyle = new ParagraphStyle(document);
hyperlinkstyle.Name = "linkStyle";
hyperlinkstyle.CharacterFormat.FontName = "Calibri";
hyperlinkstyle.CharacterFormat.FontSize = 14;
document.Styles.Add(hyperlinkstyle);

Step 3: Add the text to the paragraph with style and link it to a web page.

Para = Sec.AddParagraph();
Para.AppendText("Home page");
Para.ApplyStyle(txtStyle.Name);
Para = Sec.AddParagraph();
Para.AppendHyperlink("HomePage", "www.e-iceblue.com",HyperlinkType.WebLink);
Para.ApplyStyle(hyperlinkstyle.Name);

Step 4: Add the text to the paragraph with style and link it to an email address.

Para = Sec.AddParagraph();
Para.AppendText("Contact US");
Para.ApplyStyle(txtStyle.Name);
Para = Sec.AddParagraph();
Para.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com",HyperlinkType.EMailLink);
Para.ApplyStyle(hyperlinkstyle.Name);

Step 5: Save the document to file and launch to preview it.

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

Effective screenshot of adding hyperlinks to the word document in C#:

How to create word hyperlinks in C# on WPF applications

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document document = new Document();
            Section Sec = document.AddSection();
            Paragraph Para = Sec.AddParagraph();

            ParagraphStyle txtStyle = new ParagraphStyle(document);
            txtStyle.Name = "Style";
            txtStyle.CharacterFormat.FontName = "Calibri";
            txtStyle.CharacterFormat.FontSize = 16;
            txtStyle.CharacterFormat.TextColor = System.Drawing.Color.RosyBrown;
            document.Styles.Add(txtStyle);
            ParagraphStyle hyperlinkstyle = new ParagraphStyle(document);
            hyperlinkstyle.Name = "linkStyle";
            hyperlinkstyle.CharacterFormat.FontName = "Calibri";
            hyperlinkstyle.CharacterFormat.FontSize = 14;
            document.Styles.Add(hyperlinkstyle);

            Para = Sec.AddParagraph();
            Para.AppendText("Home page");
            Para.ApplyStyle(txtStyle.Name);

            Para = Sec.AddParagraph();
            Para.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
            Para.ApplyStyle(hyperlinkstyle.Name);

            Para = Sec.AddParagraph();
            Para.AppendText("Contact US");
            Para.ApplyStyle(txtStyle.Name);

            Para = Sec.AddParagraph();
            Para.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink);
            Para.ApplyStyle(hyperlinkstyle.Name);

            document.SaveToFile("Hyperlink.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Hyperlink.docx");
        }


    }
}