Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneRender.Pdf

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

C#
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, 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>

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.

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 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.

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.

All onerender recipes