Recursive AI

Add a hyperlink to a PDF in C#

OnePDF is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Make a text label clickable on a new PDF page. In an existing PDF, OnePDF 1.25.0 can add a link by editing the page’s annotation array, not by calling AddLink.

Terminal
dotnet add package RecursiveAI.OnePdf

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

C#
using OnePdf;
 
using var file = File.Create("link.pdf");
using var writer = new PdfDocumentWriter(file);
var page = writer.BeginPage(200, 120);
var area = new PdfRectangle(19, 57, 116, 73);
page.AddLink(area, "https://example.org/");
var font = writer.AddStandardFont("Helvetica");
page.SetFont(font, 12);
page.SetFillRgb(0, 0, 0.8);
page.DrawText("Visit example.org", 20, 60);
page.Commit();
writer.Finish();
Console.WriteLine("Wrote link.pdf");

Unzip the example, then dotnet run.

Open link.pdf and click “Visit example.org” to follow the link.

Text
Wrote link.pdf

AddLink throws ArgumentException if the rectangle is empty or its coordinates aren’t finite.

Variations

Jump within the PDF instead of opening a website. Replace the AddLink line with these two lines. This links to the named position start on the first page (pageIndex: 0); 120 is the destination’s y position in points.

C#
writer.AddNamedDestination("start", 0, 120);
page.AddLinkToDestination(area, "start");

The short answer

When creating a PDF in C#, call page.AddLink(area, "https://example.org/") on a page opened with PdfDocumentWriter.BeginPage. It writes a link annotation with a URI action, which tells the PDF viewer what address to open. Draw the label separately, then commit the page and call Finish() to complete the file. You don’t need to show the full URL on the page, and the writer doesn’t fetch it.

Use the same page coordinates for the visual content and the link rectangle. PdfRectangle takes left, bottom, right, top, not x, y, width, height. These numbers are in PDF points and y increases upward. In the example, the baseline of the label is at (20, 60). The link starts at (19, 57) and ends at (116, 73), leaving space below the baseline and above the letters. If you replace the label with a longer one or use a larger font, adjust the right and top edges so the whole label remains clickable.

The rectangle can sit over an image or a shape as well. It doesn’t draw anything: the blue text in the example comes from SetFillRgb and DrawText. For an internal jump, use the named-destination variation instead. Its target is part of the PDF, so the viewer doesn’t have to open a browser.

First check that you’re opening the saved PDF, not a screenshot or a printed copy. If the pointer only reacts beside the label, compare the page coordinates used to draw the label with the bounds passed to AddLink. A common mix-up is to give the rectangle a width where it expects a right edge.

If the clickable area is in the right place but the website doesn’t open, check the full address, including https://. AddLink writes the address into the PDF; it doesn’t check whether the site exists. The viewer decides whether to open external addresses. If it prompts before opening one, that’s separate from the link annotation itself. See the content creation guide for the streaming writer’s link and destination APIs.

All onepdf recipes