---
title: "OCR a password-protected PDF in C#"
url: https://recursiveai.org/oneocr/cookbook/ocr-password-protected-pdf/
description: "Run OCR on a password-protected PDF in C# with the known password. Extract text locally without writing a decrypted PDF. Includes a runnable sample."
version: "1.1.0"
---

# OCR a password-protected PDF in C#

**OneOcr** is a free, open-source OCR library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneOcr.Pdf) · [GitHub](https://github.com/recursive-ai-oss/oneocr-csharp) · [Docs](https://recursiveai.org/oneocr/docs/) · [Download example (.zip)](https://recursiveai.org/oneocr/cookbook/ocr-password-protected-pdf/ocr-password-protected-pdf.zip)

Pass the known password to read the “Warehouse inventory” scan in `locked.pdf` from the zip.

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

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

```csharp
using OneOcr;
using OneOcr.Pdf;
var engine = OcrEngine.LoadBundled();
var options = new PdfOcrOptions
{ Password = "sample-pass" };
var pages = PdfOcr.Read(
    "locked.pdf", engine, options);
foreach (var page in pages)
    Console.WriteLine(page.ToText());
```

Unzip [the example](https://recursiveai.org/oneocr/cookbook/ocr-password-protected-pdf/ocr-password-protected-pdf.zip), then `dotnet run`.

Prints the text recognized from the scan:

```text
Warehouse inventory
```

## The full program

The sample is an encrypted, one-page PDF with the words stored as pixels, not selectable text. The program uses the password to open the scan and prints a page number before its text.

```csharp
using OneOcr;
using OneOcr.Pdf;

var engine = OcrEngine.LoadBundled();
var options = new PdfOcrOptions
{
    Password = "sample-pass"
};
var pages = PdfOcr.Read("locked.pdf", engine, options);
foreach (var page in pages)
{
    Console.WriteLine($"Page {page.Index + 1}");
    Console.WriteLine(page.ToText());
}
```

```text
Page 1
Warehouse inventory
```

If the password is wrong, `PdfOcr.Read` throws `OcrFormatException`; that exception can also mean the PDF couldn’t be parsed. The extracted text is plain text, so don’t send it to a log that isn’t meant to hold the document’s contents.

## Variations

**Read the password from your app’s environment.** Set `PDF_OPEN_PASSWORD` to `sample-pass` for this sample and replace the `options` declaration. In your own app, supply the document password at runtime rather than putting it in source code.

```csharp
var password = Environment.GetEnvironmentVariable("PDF_OPEN_PASSWORD")
    ?? throw new InvalidOperationException("Set PDF_OPEN_PASSWORD.");
var options = new PdfOcrOptions { Password = password };
```

**Handle a PDF that won’t open.** Keep the `using` lines and `engine` from the snippet at the top, then replace the rest with this variation. It uses a deliberately wrong password for `locked.pdf`.

```csharp
try
{
    var options = new PdfOcrOptions { Password = "wrong-pass" };
    var pages = PdfOcr.Read("locked.pdf", engine, options);
    foreach (var page in pages)
        Console.WriteLine(page.ToText());
}
catch (OcrFormatException)
{
    Console.Error.WriteLine("Could not read the PDF. Check the password and file.");
}
```

Prints to standard error:

```text
Could not read the PDF. Check the password and file.
```

## Related recipes

- [Extract text from a scanned PDF](https://recursiveai.org/oneocr/cookbook/ocr-pdf/)
- [Fail when PDF OCR can’t read a region](https://recursiveai.org/oneocr/cookbook/fail-on-incomplete-pdf-ocr/)

## The short answer

Set `PdfOcrOptions.Password` to the password you already know, then pass those options to `PdfOcr.Read`. The same password is used when OneOcr opens the PDF and when it renders the scan to pixels. Call `ToText()` on each page for plain text. The example neither changes the input file nor saves a password-free copy.

## How do I open a password-protected PDF for OCR in C#?

Use the password you would enter to open the document in a viewer. In [the pinned API](https://github.com/recursive-ai-oss/oneocr-csharp/blob/9da32fbc91f5cda6f9f933bbefc3eb22183ffb3c/src/OneOcr.Pdf/PdfOcrOptions.cs#L50-L51), `Password` is documented as “The password of an encrypted document; null for the empty user password.” Leaving it unset doesn’t help when the file requires a nonempty password. Pass the options with each read; the password isn’t part of the engine, so you can reuse the same engine across documents that have different credentials.

Opening the file and recognizing text are separate steps. If opening fails, check the credential and confirm the file can be opened in a viewer. If opening succeeds but the returned text is empty or incomplete, check the page’s `Diagnostics`. For example, a scan region that exceeds the OCR limits can produce a diagnostic even though the password was accepted.

## Do I have to unlock the PDF before running OCR?

You don’t need to save an unlocked copy first. `Read` opens the original with the supplied password and returns page objects. On a scan like the sample, words come from optical character recognition (OCR), which reads the letters in the page image. On a PDF with usable selectable text, the [PDF bridge](https://recursiveai.org/oneocr/docs/pdf-bridge/) can return those existing words together with words read from image regions.

Choose where the returned text goes before processing a private document. The original PDF stays encrypted on disk, but sending `ToText()` to the console, a search index, or a text file doesn’t transfer the PDF’s password protection to that destination. Printing to the console is useful here because the sample is public.
