Add Hyperlinks and Bookmarks Dynamically using C#

Hyperlinks and bookmarks are handy for users to navigate the relevant content quickly. When we click a hyperlink, it opens the URL in the browser or navigates to the specified bookmark location within the document. In this article, we will learn how to add hyperlinks and bookmarks to reports dynamically using C#.

The following topics shall be covered in this article:

For adding hyperlinks and bookmarks to reports dynamically, we will be using GroupDocs.Assembly for .NET API. It allows you to build powerful document automation and report generation applications. It fetches data from the data source as per the defined template document, assembles it, and generates reports in the specified output format. Please either download the DLL of the API or install it using NuGet.

PM> Install-Package GroupDocs.Assembly

We can insert hyperlinks into reports dynamically using link tags. We can use the link tag in the template document as shown below:

<<link [uri_expression][display_text_expression]>>

The uri_expression defines the URI for a hyperlink, whereas the display_text_expression defines the text to be displayed for the hyperlink.

We can insert hyperlinks to reports dynamically by following the steps given below:

  1. Create an instance of the DocumentAssembler class.
  2. Call the AssembleDocument() method to assemble the document. It takes source document template path, target document path, and data source as arguments.

The following code sample shows how to add hyperlinks dynamically using C#.

Add Hyperlinks Dynamically using C#.

Add Hyperlinks Dynamically using C#.

Add Bookmarks Dynamically using C#

We can insert bookmarks into reports dynamically using bookmark tags. We can use the bookmark tag in the template document as shown below:

<<bookmark [bookmark_expression]>>
bookmarked_content
<</bookmark>>

The bookmark_expression defines the name of a bookmark to be inserted.

We can insert bookmarks to reports dynamically by following the steps given below:

  1. Create an instance of the DocumentAssembler class.
  2. Call the AssembleDocument() method to assemble the document. It takes source document template path, target document path, and data source as arguments.

The following code sample shows how to add bookmarks dynamically using C#.

We can also link bookmarks to hyperlinks within a document by following the steps mentioned earlier. However, we just need to set the same value for the uriExpression and bookmark_expression as shown below:

const string bookmark_expression = "gd_bookmark";
const String uriExpression = "gd_bookmark";

The following code sample shows how to insert a bookmark as a hyperlink using C#.

Insert Bookmark as Hyperlink using C#.

Insert Bookmark as Hyperlink using C#.

Get a Free License

Please try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to:

  • insert hyperlinks and bookmarks to reports dynamically using C#;
  • add a hyperlink to a bookmark dynamically.

Besides, you can learn more about GroupDcos.Assembly for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also