Recursive AI

Extract hyperlinks from 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.

Read clickable URLs and page jumps, not addresses printed as text. Try links.pdf from the download.

Terminal
dotnet add package RecursiveAI.OnePdf

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

A link annotation is a clickable area: a URI action opens an address, a GoTo action jumps within this PDF, and GoToR or GoToE points into another PDF.

C#
using OnePdf;
 
using var doc =
    PdfDocument.Open("links.pdf");
foreach (var page in doc.Pages)
foreach (var link in page.Links)
{
    if (link.Uri is string uri)
        Console.WriteLine(uri);
    // A jump to a page in this PDF.
    else if (link.Action?.ActionType
        is null or "GoTo" &&
        link.Destination is
        { IsRemotePage: false,
          PageIndex: >= 0 and int index })
        Console.WriteLine(
            $"Page {index + 1}");
}

Unzip the example, then dotnet run.

The snippet prints the URL and then the page number of the internal jump:

Text
https://example.org/
Page 2

The full program

The sample has two clickable labels on page 1: “Visit Example Domain” and “Jump to Appendix.” The downloaded Program.cs also prints the page each link was found on. It reports links to other PDFs and links without a URL or a resolved page instead of treating them as local jumps. To read another file, run dotnet run -- your.pdf.

C#
using OnePdf;
 
string path = args.Length == 0 ? "links.pdf" : args[0];
using var doc = PdfDocument.Open(path);
int count = 0;
 
foreach (var page in doc.Pages)
{
    foreach (var link in page.Links)
    {
        count++;
        string? action = link.Action?.ActionType;
        string target = link switch
        {
            { Uri: string uri } => $"URI {uri}",
            _ when action is "GoToR" or "GoToE" => $"other PDF ({action})",
            { Destination: { IsRemotePage: false, PageIndex: >= 0 } dest }
                => $"page {dest.PageIndex + 1}",
            _ => $"no local target ({action ?? "no action"})"
        };
        Console.WriteLine($"Page {page.Index + 1}: {target}");
    }
}
 
if (count == 0)
    Console.WriteLine("No link annotations found.");

dotnet run prints:

Text
Page 1: URI https://example.org/
Page 1: page 2

If a PDF requires a password and you don’t supply the correct one in PdfOpenOptions.Password, Open throws PdfPasswordException before any links are read.

Variations

List each URI once, without internal jumps. This keeps the first occurrence of each exact address; it doesn’t normalize URLs.

C#
using OnePdf;
 
using var doc = PdfDocument.Open("links.pdf");
var seen = new HashSet<string>(StringComparer.Ordinal);
foreach (var page in doc.Pages)
foreach (var link in page.Links)
{
    if (link.Uri is string uri && seen.Add(uri))
        Console.WriteLine(uri);
}
Text
https://example.org/

The short answer

To extract hyperlinks from a PDF in C#, open it with PdfDocument.Open and loop through page.Links on each page. Each item is a link annotation: the PDF record that connects a clickable area to a target. Read Uri for an address and Destination for an internal jump. For the latter, add one to PageIndex to get the numbered page in file order. The example uses the free, open-source RecursiveAI.OnePdf package and runs without opening a browser.

Read the annotation’s Uri rather than searching for https:// in the page text. In links.pdf, the label “Visit Example Domain” contains no address. The PDF stores https://example.org/ in the link’s URI action, the instruction that tells a viewer where to go. Conversely, the line “Plain text: https://example.net/” has no link annotation. It doesn’t appear in the output even if your viewer spots the address and makes it clickable on screen.

OnePDF returns the address as stored. That’s useful when you need to compare what two files actually contain; the same link can be printed more than once if it appears on several pages. Use the unique-URI variation if you want one row per exact address. If you want URLs printed on the page as well, treat text extraction as a separate pass. That pass can find visible addresses, but it can’t tell you the target hidden behind a label like “Visit Example Domain.”

A PDF viewer can follow a stored link when you click it. OnePDF reads the target as data; it doesn’t fetch URI addresses or perform actions. A link with no Uri isn’t necessarily broken. The “Jump to Appendix” link is a GoTo action: it points to a named destination, which OnePDF resolves to page 2. Other PDFs can store the destination on the annotation itself (/Dest); the same Destination property reads it.

A valid local destination has a nonnegative PageIndex and isn’t marked as a remote page. In the sample output, “Page 1: page 2” means you click on page 1 and land on page 2. Those numbers describe file order, not labels printed on the pages. A GoToR or GoToE action points to a separate or embedded PDF, not to a page of the current file, so the program reports it separately. If a destination doesn’t resolve, don’t turn index -1 into “page 0”; report the link without a local page number.

All onepdf recipes