Recursive AI

Block file and external links in HTML to PDF with 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.

Use InternalOnly to keep in-document jumps, or Web to keep website links too. Both block file links.

Terminal
dotnet add package RecursiveAI.OneRender.Pdf

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

C#
using OneRender.Pdf;
 
// links.html is shown below and in the zip.
var html = File.ReadAllText("links.html");
var options = new HtmlPdfOptions
{
    Output = new()
    { Links = PdfLinkPolicy.InternalOnly }
};
File.WriteAllBytes("internal.pdf",
    HtmlPdf.ToPdf(html, options));

Writes internal.pdf with only the “Notes” link active.

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

The full program

Save this input as links.html alongside Program.cs, or use the copy in the zip. It has an in-document anchor, a website link and a local file link. The program renders it twice so you can open the two PDFs side by side.

HTML
<a href="#notes">Notes</a>
<a href="https://example.com/">Website</a>
<a href="file:///example/notes.txt">File</a>
<h2 id="notes">Meeting notes</h2>
C#
using OneRender.Pdf;
 
var html = File.ReadAllText("links.html");
Save("internal.pdf",
    PdfLinkPolicy.InternalOnly);
Save("web.pdf", PdfLinkPolicy.Web);
 
void Save(string path, PdfLinkPolicy policy)
{
    var options = new HtmlPdfOptions
    { Output = new() { Links = policy } };
    File.WriteAllBytes(path, HtmlPdf.ToPdf(html, options));
    Console.WriteLine($"{path}: {policy}");
}
Text
internal.pdf: InternalOnly
web.pdf: Web

In internal.pdf only “Notes” is a link. In web.pdf, “Notes” and “Website” are links. “File” is still printed in both PDFs but has no link annotation, the clickable area the writer would otherwise add. If “Notes” doesn’t jump, check that id="notes" matches the fragment in the link and that the heading is visible in print.

Variations

Turn off even in-document jumps. With the Save function in the full program, add this call alongside the other two:

C#
Save("no-links.pdf", PdfLinkPolicy.None);

The short answer

Set HtmlPdfOptions.Output.Links to PdfLinkPolicy.InternalOnly before calling HtmlPdf.ToPdf to block file links and all other external links. Use PdfLinkPolicy.Web if you still want website links to work. Neither setting removes the words that were inside the link, so a references list is still readable even when readers can’t follow its external links.

Yes. Choose the policy according to where you want readers to be able to go:

PolicyClickable links OneRender writes
All (default)In-document anchors and external addresses, including file:
WebIn-document anchors and addresses with an http, https, mailto or tel scheme
InternalOnlyIn-document anchors only
NoneNone

“Internal” means a fragment such as #notes that points to a rendered destination in this PDF. It doesn’t mean a link to another report on the same server or another file in the same folder. A full URL ending in #notes is still external. With Web, write out the scheme for links you want to keep: a relative href="notes.html" or a scheme-relative href="//example.com/notes" doesn’t pass the allowlist. Web also allows email and phone links, so it isn’t an HTTPS-only filter.

When the PDF is made from HTML, choose None at render time to suppress the annotations instead of rewriting the <a> elements; this keeps their rendered text and styling. The PDF output docs note that “The link text stays marked as Link in the structure tree either way.” In a tagged PDF, that tree describes the role of the text independently of whether there is a clickable annotation. If you want suppressed links to look like ordinary text, change the link styles in the HTML as well. This setting doesn’t edit an existing PDF.

This option controls only the link annotations OneRender writes. A PDF viewer may recognize a printed URL and make it clickable independently. If a file path or address must not appear in the PDF, remove it from the visible HTML before rendering, rather than relying on a link policy. In the sample, the labels are “Notes,” “Website” and “File,” not the literal addresses.

All onerender recipes