Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneOcr.Pdf

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

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

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

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

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

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

All oneocr recipes