Recursive AI

Add a hyperlink to a Word document in C#

OneDocument is a free, open-source Word, Excel and PowerPoint document library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Add a clickable link with its own display text to a new DOCX.

Terminal
dotnet add package \
    RecursiveAI.OneDocument.Documents

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneDocument.Documents;
using var doc = Document.Create();
doc.Body.AddParagraph("").AddHyperlink(
    "the guide",
    "https://example.com/guide");
doc.SaveAs("links.docx");
Console.WriteLine("Wrote links.docx");
Text
Wrote links.docx

The DOCX shows “the guide” as a clickable link.

Unzip the example, then dotnet run.

The full program

The download puts the link inside a sentence: “See the guide for details.” Only “the guide” is linked. It uses a reserved example URL, and no request is sent to it.

C#
using RecursiveAI.OneDocument.Documents;
 
using var doc = Document.Create();
var paragraph = doc.Body.AddParagraph("See ");
paragraph.AddHyperlink(
    "the guide", "https://example.com/guide");
paragraph.AddRun(" for details.");
doc.SaveAs("links.docx");
Console.WriteLine("Wrote links.docx");

dotnet run prints:

Text
Wrote links.docx

A blank destination throws ArgumentException; a nonblank address being saved does not mean it points to a reachable page.

Variations

Show the URL itself. Replace the AddHyperlink call in the full program with this if the reader needs to see the address as well as click it:

C#
const string url = "https://example.com/guide";
paragraph.AddHyperlink(url, url);

The short answer

To add a hyperlink to a Word document in C#, install RecursiveAI.OneDocument.Documents, call paragraph.AddHyperlink(text, address), then save the document as .docx. The first argument is what the reader sees; the second is where the link goes.

A hyperlink belongs to a paragraph. For a link on its own line, start with an empty paragraph, as in the snippet at the top. For a link in the middle of a sentence, start with the text before the link, append the link, then use AddRun for the text after it. Include any spaces or punctuation that belong outside the link in those surrounding pieces. AddHyperlink appends at the end of the paragraph; it does not look for matching words earlier in the text and turn them into a link.

OneDocument uses the Hyperlink character style for the link text and adds the style if the document lacks it. The style and the destination are separate: changing how text looks does not change where it leads. For an external URL, the DOCX stores the destination in a relationship, a separate entry referenced by the hyperlink. OneDocument stores that address without fetching it, so the file can be generated offline. If you generate documents from a list of links, check the destinations before writing if they must be valid; successfully saving a DOCX does not check them for you.

If your document already has a bookmark named Details, pass #Details as the address, not a web URL. For example, replace the AddHyperlink call in the full program with:

C#
paragraph.AddHyperlink("the details", "#Details");

The leading # tells OneDocument to write an internal anchor instead of an external relationship. The text after # is the bookmark name. The call does not add the bookmark itself. Use this in a document that already has that destination; the fresh document in the download does not. To point to a section of a web page instead, put the full URL first and the fragment at the end, as in https://example.com/guide#details. That address does not start with #, so it is stored as an external link.

All onedocument recipes