A hyperlink is an anchored text that leads to an online web page when it is clicked. You can make any text in a Word document a hyperlink that will navigate the users to the linked page when clicked. Hyperlinks make it easy for writers to guide the document readers for any references that are linked to the main article. Being a .NET application developer, you can add the feature of adding hyperlinks to your document processing application in C#.

In this article, we will be going through a step-by-step tutorial to learn about inserting a hyperlink using NPOI for .NET in C#.

Before proceeding to see how to insert a hyperlink in DOCX using C#, let’s have a look at how the same can be achieved using Microsoft Word.

You can hyperlink in a document using Microsoft Word as shown in the following steps:

  1. Select the text or picture that you want to display as a hyperlink.
  2. On the ribbon, on the Insert tab, select Link. You can also right-click the text or picture and click the Link on the shortcut menu.
  3. In the **Insert Hyperlink **box, type or paste your link in the Address box.

Now that you have seen how to insert hyperlinks in a Word document using Microsoft Word, we are ready to have a look at how the same can be achieved using NPOI for .NET in our C# application. You can go through our comprehensive guide for NPOI and its installation guidelines just in case you haven’t configured it yet.

At this stage, we expect that your C# console application is ready with NPOI API added to it. Use the following steps to hyperlink text in Word Document using NPOI API in C#.

  1. Create an instance of XWPFDocument class
  2. Add a paragraph and text using instances of XWPFParagraph and XWPFRun
  3. Create a Hyperlink instance of XWPFHyperlinkRun for the defined paragraph
  4. Use the CreateHyperlinkRun method on paragraph instance to hyperlink

You can copy the following code (taken from nissl lab from Github) in your application to try it.

static void InsertHyperlink()
{
    using (XWPFDocument doc = new XWPFDocument())
    {
        XWPFParagraph paragraph = doc.CreateParagraph();
        XWPFRun run = paragraph.CreateRun();
        run.SetText("This is a text paragraph having ");
        XWPFHyperlinkRun hyperlinkrun = CreateHyperlinkRun(paragraph, "https://www.google.com");
        hyperlinkrun.SetText("a link to Google");
        hyperlinkrun.SetColor("0000FF");
        hyperlinkrun.Underline = UnderlinePatterns.Single;
        run = paragraph.CreateRun();
        run.SetText(" in it.");
        using (FileStream out1 = new FileStream("hyperlink.docx", FileMode.Create))
        {
            doc.Write(out1);
        }
    }
}
static XWPFHyperlinkRun CreateHyperlinkRun(XWPFParagraph paragraph, String uri)
{
    String rId = paragraph.Document.GetPackagePart().AddExternalRelationship(
        uri,
        XWPFRelation.HYPERLINK.Relation
        ).Id;

    return paragraph.CreateHyperlinkRun(rId);
}

Conclusion

Hyperlinks are an effective way to create links between a document and web link. Application developers who provide the functionality of document processing. in their .NET applications can use NPOI API for .NET to provide the functionality of hyperlinking text in Word documents in C#. For more examples of working with NPOI API in C#, stay tuned to this blog.