---
title: "Block file and external links in HTML to PDF with C#"
url: https://recursiveai.org/onerender/cookbook/remove-external-links-from-html-pdf/
description: "Block file links and external hyperlinks when converting HTML to PDF in C#. Keep only in-document jumps, or allow web links while blocking file links."
version: "1.6.4"
---

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

[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/remove-external-links-from-html-pdf/remove-external-links-from-html-pdf.zip)

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

```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;

// 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](https://recursiveai.org/onerender/cookbook/remove-external-links-from-html-pdf/remove-external-links-from-html-pdf.zip) 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>
```

```csharp
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:

```csharp
Save("no-links.pdf", PdfLinkPolicy.None);
```

## Related recipes

- [Keep internal links when converting HTML to PDF](https://recursiveai.org/onerender/cookbook/render-html-anchor-links-in-pdf/)
- [Convert HTML to PDF in C#](https://recursiveai.org/onerender/cookbook/html-to-pdf/)

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

## Can HTML to PDF preserve clickable links without keeping file links?

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

| Policy | Clickable links OneRender writes |
| --- | --- |
| `All` (default) | In-document anchors and external addresses, including `file:` |
| `Web` | In-document anchors and addresses with an `http`, `https`, `mailto` or `tel` scheme |
| `InternalOnly` | In-document anchors only |
| `None` | None |

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

## How do I remove clickable links in a PDF without deleting the text?

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](https://recursiveai.org/onerender/docs/pdf/#outline-and-links) 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.

## How do I stop text from turning into a hyperlink?

This option controls only the link annotations OneRender writes. A PDF viewer may [recognize a printed URL and make it clickable independently](https://helpx.adobe.com/acrobat/using/viewing-pdfs-viewing-preferences.html). 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.
