---
title: "Throw on unreadable PDF pages in C#"
url: https://recursiveai.org/oneocr/cookbook/fail-on-incomplete-pdf-ocr/
description: "Set Strict on OneOcr’s PDF reader to reject incomplete OCR results in C#. See the diagnostic and exception produced by a PDF with no readable pages."
version: "1.1.0"
---

# Throw on unreadable PDF pages 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/fail-on-incomplete-pdf-ocr/fail-on-incomplete-pdf-ocr.zip)

Reject incomplete OCR with strict mode. `broken.pdf` in the zip has no readable pages.

```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();
PdfOcr.Read("broken.pdf", engine,
    new PdfOcrOptions { Strict = true });
```

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

Throws `OcrFormatException`: `The PDF has no readable pages: its page tree is missing or damaged.`

## The full program

`broken.pdf` from the zip points to a missing `/Pages` object: This is damage to the page tree, not a blank scan. The program reads it first with default options, then catches the exception from the strict read.

```text
%PDF-1.7
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
trailer
<< /Root 1 0 R >>
%%EOF
```

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

var engine = OcrEngine.LoadBundled();
var pages = PdfOcr.Read("broken.pdf", engine);
Console.WriteLine($"Default: {pages.Count} result, {pages.Sum(p => p.Words.Count)} words");
foreach (var diagnostic in pages.SelectMany(p => p.Diagnostics))
    Console.WriteLine(diagnostic.Message);

try
{
    var strict = new PdfOcrOptions { Strict = true };
    PdfOcr.Read("broken.pdf", engine, strict);
}
catch (OcrFormatException error)
{
    Console.WriteLine($"Rejected: {error.GetType().Name}");
    Console.WriteLine(error.Message);
}
```

```text
Default: 1 result, 0 words
The PDF has no readable pages: its page tree is missing or damaged.
Rejected: OcrFormatException
The PDF has no readable pages: its page tree is missing or damaged.
```

That one result in default mode is an empty placeholder carrying the warning, not a recovered PDF page. A file that can’t be opened at all can throw `OcrFormatException` even without strict mode.

## Variations

**Keep partial results for review instead of rejecting the file.** After loading `engine` as above, read with default options and keep the diagnostics alongside the text:

```csharp
var pages = PdfOcr.Read("broken.pdf", engine);
foreach (var page in pages)
    foreach (var note in page.Diagnostics)
        Console.WriteLine(note.Message);
```

```text
The PDF has no readable pages: its page tree is missing or damaged.
```

**Reject a two-page PDF with one page that can’t render.** In `damaged-page.pdf` from the zip, page 2 has an erroneous page width that exceeds the renderer’s dimension limit at the default DPI. After loading `engine` as above, read just page 1, then try the whole file:

```csharp
const string path = "damaged-page.pdf";
var first = PdfOcr.Read(path, engine,
    new PdfOcrOptions { Strict = true, PageCount = 1 });
Console.WriteLine(first[0].Text);
try
{
    PdfOcr.Read(path, engine, new PdfOcrOptions
    {
        Strict = true, MaxDegreeOfParallelism = 1
    });
}
catch (OcrLimitException error)
{
    Console.WriteLine(error.GetType().Name);
}
```

```text
Readable page
OcrLimitException
```

The whole-file call throws rather than returning page 1 by itself. In this case the failure is a render limit, not a missing page tree.

## Related recipes

- [OCR a PDF in C#](https://recursiveai.org/oneocr/cookbook/ocr-pdf/)
- [Make a scanned PDF searchable in C#](https://recursiveai.org/oneocr/cookbook/make-scanned-pdf-searchable/)

## The short answer

Pass `new PdfOcrOptions { Strict = true }` to `PdfOcr.Read` to fail the read instead of accepting partial results when a PDF page cannot be fully processed. With the default options, recoverable problems can leave you with some text and a warning. Put the strict read before writing the transcript or updating a search index. If the read throws, record the failed file and don’t publish it as a completed import.

## Why is OCR not working on my PDF?

Check whether the PDF opens and whether you can navigate to the expected pages. A damaged page tree, like the one in this example, leaves no page to render or recognize. Raising the rendering DPI won’t make missing page objects reappear. Go back to the original export or scan if you can.

If pages do open, inspect the diagnostics instead of deciding from an empty string. A readable page might be blank; a damaged content stream or a region that exceeds an OCR limit can also leave no words. Default mode can keep readable content when only part of the page fails. Strict mode changes how those failures are reported; it does not make the engine recognize every character on a readable page. See [the strict option at v1.1.0](https://github.com/recursive-ai-oss/oneocr-csharp/blob/9da32fbc91f5cda6f9f933bbefc3eb22183ffb3c/src/OneOcr.Pdf/PdfOcrOptions.cs#L53-L57).

## How do I detect incomplete PDF OCR in a C# batch?

Choose the rule by what you do with the result. If partial text is useful to reviewers, preserve the page diagnostics and mark the result for review. If downstream users need a completed read, enable strict mode and handle the exception per file, outside the call that reads the document. A format problem and a resource limit are not the same failure: the API also documents `OcrLimitException` for limits that can stop a strict read. An OCR read that returns successfully is still not a spelling or accuracy check. If your job requires particular page counts or fields, validate those separately before saving the result.
