---
title: "Keep internal links when converting HTML to PDF in C#"
url: https://recursiveai.org/onerender/cookbook/render-html-anchor-links-in-pdf/
description: "Preserve clickable HTML anchor links in a PDF with C#. Match href fragments to element IDs and use OneRender to make links that jump between pages."
version: "1.4.0"
---

# Keep internal links when converting HTML to PDF in C#

**OneRender** is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Pdf) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/render-html-anchor-links-in-pdf/render-html-anchor-links-in-pdf.zip)

Link to an element’s `id` with `href="#id"`. OneRender turns the link into a jump within the PDF. JavaScript doesn’t run.

```shell
dotnet add package RecursiveAI.OneRender.Pdf
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneRender.Pdf;

string html = """
<a href="#notes">Jump to notes</a>
<h2 id="notes"
    style="break-before:page">Notes</h2>
""";
using var file = File.Create("notes.pdf");
var result = HtmlPdf.ToPdf(html, file);
Console.WriteLine(
    $"notes.pdf: {result.PageCount} pages");
```

Unzip [the example](https://recursiveai.org/onerender/cookbook/render-html-anchor-links-in-pdf/render-html-anchor-links-in-pdf.zip), then `dotnet run`.

```text
notes.pdf: 2 pages
```

Open `notes.pdf` and click “Jump to notes” on page 1 to go to “Notes” on page 2. If a link doesn’t jump, check that its `href` matches the target’s `id` and that the target is visible in the print layout.

## Variations

**Add a return link.** Add `id="start"` to the first link and put a link back to it below the heading:

```html
<a id="start" href="#notes">Jump to notes</a>
<h2 id="notes" style="break-before:page">Notes</h2>
<a href="#start">Back to start</a>
```

## Related recipes

- [Convert HTML to PDF in C#](https://recursiveai.org/onerender/cookbook/html-to-pdf/)
- [Convert HTML to PDF with a table of contents](https://recursiveai.org/onerender/cookbook/html-to-pdf-table-of-contents/)

## The short answer

To keep internal anchor links when converting HTML to PDF in C#, pass HTML containing both the link (`href="#notes"`) and the destination (`id="notes"`) to `HtmlPdf.ToPdf`. You don’t need to know which page the heading will land on. OneRender puts a jump target in the PDF where the heading lands and points the link there.

## How do I keep clickable links when converting HTML to PDF?

Use a real `<a>` element with an `href` that starts with `#`, rather than text styled to look like a link or an `onclick` handler. The [OneRender PDF docs](https://recursiveai.org/onerender/docs/pdf/#pages) call these “link annotations to named destinations the writer adds at the position of each target.” The annotation is the clickable area in the PDF; the named destination is the place it jumps to. The example starts a second page with `break-before: page` so you can see the jump. The break is not needed for the link itself.

For links that need to work in both directions, name a destination at each end. The variation makes the first link a destination too, so a reader can return from page 2 without scrolling. Write `href="#notes"`, not `href="https://example.com/page#notes"`: the full URL becomes an external link.

## Why don’t my internal links work in the PDF?

Check both ends in the HTML you actually convert. A link to `#notes` needs an element with `id="notes"` in that document. If a print stylesheet hides the heading, link to a visible element that stays in the PDF instead. Generate both the link and the ID from the same value if you’re building the HTML from a template, and give each destination a distinct ID.

A printed page number is not a link. The PDF docs note that “`target-counter()` and `target-text()` make no link by themselves.” If you want a table of contents that shows a number *and* jumps there, keep the anchor element and use generated CSS content for the number. The two work independently: a link can jump with no number at all, as in this recipe.
