Reject incomplete OCR with strict mode. broken.pdf in the zip has no readable pages.
dotnet add package RecursiveAI.OneOcr.PdfRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneOcr; using OneOcr.Pdf; var engine = OcrEngine.LoadBundled(); PdfOcr.Read("broken.pdf", engine, new PdfOcrOptions { Strict = true });
Unzip the example, 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.
%PDF-1.7 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj trailer << /Root 1 0 R >> %%EOF
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); }
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:
var pages = PdfOcr.Read("broken.pdf", engine); foreach (var page in pages) foreach (var note in page.Diagnostics) Console.WriteLine(note.Message);
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:
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); }
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
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.
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.