Add a hyperlink
Hyperlinks let readers open references and related resources directly from a PDF. Create a PDF containing a clickable web link. This example follows HyperlinkPdf.cs and saves the result as HyperlinkPdf_output.pdf.
What you’ll create
- A Letter-sized PDF containing “This is a link to dynamicpdf.com”.
- A blue, underlined link in 18-point Arial that opens
https://www.dynamicpdf.com/. - A file named
HyperlinkPdf_output.pdfin your chosen output directory.
Example code
Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class adds a hyperlink and saves the document to an existing output directory.
using System;
using System.IO;
using DynamicPDF.Flow;
namespace FluentExamples.Examples;
internal static class HyperlinkPdf
{
public static void Run(string outputRoot)
{
var path = Path.Combine(
outputRoot, "HyperlinkPdf_output.pdf");
var link = "<a href='https://www.dynamicpdf.com/' "
+ "style='color:blue;text-decoration:underline;"
+ "font-family:Arial;font-size:18pt'>"
+ "This is a link to dynamicpdf.com</a>";
DocumentBuilder.Create()
.Pages(page => page
.Size(PageSize.Letter)
.Margins(50, LengthUnit.Point)
.Text(link, text => text
.Margin(50, 0, 0, 50, LengthUnit.Point)))
.Save(path);
Console.WriteLine($"Created {path}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow
Friend Module HyperlinkPdf
Public Sub Run(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "HyperlinkPdf_output.pdf")
Dim link = "<a href='https://www.dynamicpdf.com/' " &
"style='color:blue;text-decoration:underline;" &
"font-family:Arial;font-size:18pt'>" &
"This is a link to dynamicpdf.com</a>"
DocumentBuilder.Create().
Pages(Sub(page) page.
Size(PageSize.Letter).
Margins(50, unit:=LengthUnit.Point).
Text(link, Sub(text) text.
Margin(50, 0, 0, 50, LengthUnit.Point))).
Save(outputPath)
Console.WriteLine($"Created {outputPath}")
End Sub
End Module
How it works
1. Choose where to save the PDF
Path.Combine joins outputRoot with HyperlinkPdf_output.pdf. Make sure the output directory exists. Saving overwrites an existing file with the same name.
2. Define the hyperlink
The HTML anchor's href sets the destination to https://www.dynamicpdf.com/. Its text supplies the visible label. Inline CSS selects blue, underlined, 18-point Arial text. Arial must be available in the environment that generates the PDF; an unavailable font may be replaced by a fallback.
3. Add the link to the page
DocumentBuilder.Create() starts the document. Pages adds a Letter-sized page with 50-point margins. Text renders the HTML anchor and creates its clickable link annotation. The text's additional top and left margins place the content 100 points from the page's top and left edges.
4. Save the PDF
.Save(path) saves through the fluent builder. Console.WriteLine prints the output path. Open the PDF in a viewer that supports web links to follow the hyperlink.
Next steps
Change the anchor's destination and visible text to link to your own website. Explore Create a PDF, or browse all examples.