Insert Hyperlinks Dynamically

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Assembly MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-assembly -- dnx GroupDocs.Assembly.Mcp --yes
Folders, licensing and more →

Overview

GroupDocs.Assembly for .NET enables you to insert hyperlinks dynamically into documents using link tags. Hyperlinks can point to external URLs, bookmarks within the document, or specific cells/slides depending on the document type.

The main classes involved are:

Here are the steps to insert hyperlinks dynamically:

  • Create a template document with link tags
  • Use the syntax <<link [uri_expression][display_text_expression]>>
  • Provide URI and optional display text from your data source
  • Assemble the document
Note
Link tags are replaced with clickable hyperlinks during document assembly. The URI expression is mandatory, while display text is optional.

Insert a hyperlink with both URL and display text:

using GroupDocs.Assembly;

public class Website
{
    public string Name { get; set; }
    public string Url { get; set; }
}

public static void InsertHyperlink()
{
    var website = new Website
    {
        Name = "Visit our website",
        Url = "https://www.example.com"
    };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument("HyperlinkTemplate.docx", "HyperlinkOutput.docx", 
        new DataSourceInfo(website, "website"));
    
    Console.WriteLine("Hyperlink inserted successfully.");
}

In your template (HyperlinkTemplate.docx), use link tags:

Please <<link [website.Url][website.Name]>> for more information.

After assembly:

Please Visit our website for more information.

(The text “Visit our website” will be a clickable hyperlink.)

If you omit the display text, the URL is used as the display text:

using GroupDocs.Assembly;

public static void InsertHyperlinkUrlOnly()
{
    var website = new Website
    {
        Url = "https://www.example.com"
    };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument("HyperlinkTemplate.docx", "HyperlinkOutput.docx", 
        new DataSourceInfo(website, "website"));
    
    Console.WriteLine("Hyperlink with URL only inserted successfully.");
}

Template:

Visit <<link [website.Url]>> for more information.

Insert hyperlinks for each item in a collection:

using GroupDocs.Assembly;
using System.Collections.Generic;

public static void InsertHyperlinksFromCollection()
{
    List<Website> websites = new List<Website>
    {
        new Website { Name = "Home", Url = "https://www.example.com" },
        new Website { Name = "About", Url = "https://www.example.com/about" },
        new Website { Name = "Contact", Url = "https://www.example.com/contact" }
    };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument("HyperlinksTemplate.docx", "HyperlinksOutput.docx", 
        new DataSourceInfo(websites, "websites"));
    
    Console.WriteLine("Hyperlinks inserted from collection successfully.");
}

Template with foreach:

Links:
<<foreach [in websites]>>
- <<link [Url][Name]>>
<</foreach>>

Link to bookmarks within the same document:

using GroupDocs.Assembly;

public class Section
{
    public string Title { get; set; }
    public string BookmarkName { get; set; }
}

public static void InsertBookmarkLink()
{
    var section = new Section
    {
        Title = "Go to Introduction",
        BookmarkName = "Introduction"
    };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument("BookmarkLinkTemplate.docx", "BookmarkLinkOutput.docx", 
        new DataSourceInfo(section, "section"));
    
    Console.WriteLine("Bookmark hyperlink inserted successfully.");
}

Template:

<<link [section.BookmarkName][section.Title]>>

Link to cells in spreadsheet documents:

using GroupDocs.Assembly;

public static void InsertCellLink()
{
    var data = new { CellReference = "A1", DisplayText = "Go to Home" };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument("CellLinkTemplate.xlsx", "CellLinkOutput.xlsx", 
        new DataSourceInfo(data, "data"));
    
    Console.WriteLine("Cell hyperlink inserted successfully.");
}

Template:

<<link ["A1"]["Go to Home"]>>

Link to slides in presentation documents:

using GroupDocs.Assembly;

public static void InsertSlideLink()
{
    var data = new { SlideNumber = "Slide1", DisplayText = "Go to First Slide" };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument("SlideLinkTemplate.pptx", "SlideLinkOutput.pptx", 
        new DataSourceInfo(data, "data"));
    
    Console.WriteLine("Slide hyperlink inserted successfully.");
}

Template:

<<link ["Slide1"]["Go to First Slide"]>>

Using Uri Objects

You can use Uri objects directly:

using GroupDocs.Assembly;
using System;

public class Website
{
    public Uri Url { get; set; }
    public string Name { get; set; }
}

public static void InsertHyperlinkWithUri()
{
    var website = new Website
    {
        Url = new Uri("https://www.example.com"),
        Name = "Visit our website"
    };
    
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument("HyperlinkTemplate.docx", "HyperlinkOutput.docx", 
        new DataSourceInfo(website, "website"));
    
    Console.WriteLine("Hyperlink with Uri object inserted successfully.");
}
Warning
Link tags cannot be used within charts. For hyperlinks in charts, use alternative approaches or place links outside chart elements.

Advanced Usage Topics

To learn more about bookmark creation, complex hyperlink scenarios, and advanced link formatting, please refer to the advanced usage section.

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Apps

Along with the full-featured .NET library, we provide simple but powerful free online apps.

To assemble documents from templates and data sources, you can use the online apps from the GroupDocs.Assembly App Product Family.