---
title: "Convert a password-protected PDF to HTML in C#"
url: https://recursiveai.org/onepdf-html/cookbook/convert-password-protected-pdf-to-html/
description: "Convert a password-protected PDF to HTML in C# with an authorized password. Handle a missing or incorrect password with PdfPasswordException."
version: "1.0.0"
---

# Convert a password-protected PDF to HTML in C#

**OnePdf.Html** is a free, open-source .NET library from Recursive AI that converts PDF to HTML and HTML to PDF. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf.Html) · [GitHub](https://github.com/recursive-ai-oss/onepdf-html-csharp) · [Docs](https://recursiveai.org/onepdf-html/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf-html/cookbook/convert-password-protected-pdf-to-html/convert-password-protected-pdf-to-html.zip)

Use `locked.pdf` from the example zip with `demo-open`, or use your own `locked.pdf` and its password.

```shell
dotnet add package RecursiveAI.OnePdf.Html
```

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

```csharp
using OnePdf.Html;
PdfHtml.Convert("locked.pdf", "unlocked.html",
    new() { Password = "demo-open" });
```

Writes `unlocked.html`.

[The example](https://recursiveai.org/onepdf-html/cookbook/convert-password-protected-pdf-to-html/convert-password-protected-pdf-to-html.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

The encrypted sample contains one line, **Sample invoice**. The program tries the correct password, no password, and an intentionally wrong password, using a different output filename for each attempt.

```csharp
using OnePdf;
using OnePdf.Html;

TryConvert("demo-open", "unlocked.html");
TryConvert(null, "missing.html");
TryConvert("not-the-password", "wrong.html");

static void TryConvert(string? password, string output)
{
    try
    {
        var options = new PdfHtmlOptions { Password = password };
        var result = PdfHtml.Convert("locked.pdf", output, options);
        Console.WriteLine($"Wrote {output} ({result.PageCount} page).");
    }
    catch (PdfPasswordException)
    {
        Console.WriteLine($"{output}: a valid PDF password is required.");
    }
}
```

```text
Wrote unlocked.html (1 page).
missing.html: a valid PDF password is required.
wrong.html: a valid PDF password is required.
```

A wrong or missing password throws `OnePdf.PdfPasswordException` when the PDF opens; neither failed attempt creates an HTML file. The HTML export is not password protected, so store it somewhere only authorized readers can access.

## Variations

Replace the `new() { ... }` argument in the snippet at the top to keep the original page dimensions and positions rather than letting content reflow:

```csharp
new PdfHtmlOptions
{
    Password = "demo-open",
    Mode = PdfHtmlMode.FixedLayout,
}
```

## Related recipes

- [Convert PDF to HTML and keep the layout](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html-keeping-layout/)
- [Convert selected PDF pages to HTML](https://recursiveai.org/onepdf-html/cookbook/convert-selected-pdf-pages-to-html/)

## The short answer

Set `PdfHtmlOptions.Password` to a password you’re authorized to use, then call `PdfHtml.Convert` with the original PDF and the destination HTML path. You don’t need to write a separate decrypted PDF. Catch `PdfPasswordException` around the conversion so your app can ask for the password again or stop without reporting success.

## Why is my password-protected PDF not opening?

For a PDF that requires a password, an omitted password and an incorrect password produce the same exception. The converter tries the value you supply as the user password, then as the owner password. It doesn’t need a flag to tell it which one your document owner supplied.

Keep that exception separate from `PdfFormatException`, which can mean the file isn’t a readable PDF at all. In an upload workflow, an unreadable file needs a different response from a password prompt. Don’t include the attempted password in a log or in a message that goes back to the caller. `demo-open` is only for the downloadable sample; use the authorized caller’s password for their file.

## Do I need to convert a password-protected PDF to an unlocked PDF first?

No. The converter opens the encrypted PDF and writes HTML directly. `Password` works whether the output is reflowing text or fixed-layout pages. The password is an input to PDF opening, not a setting on the HTML destination. If you’re serving the result as a preview, keep the preview behind the same access controls as the source; it contains readable document content even though the original PDF still requires a password.
