Lyo.Pdf.Ocr
2.0.0
dotnet add package Lyo.Pdf.Ocr --version 2.0.0
NuGet\Install-Package Lyo.Pdf.Ocr -Version 2.0.0
<PackageReference Include="Lyo.Pdf.Ocr" Version="2.0.0" />
<PackageVersion Include="Lyo.Pdf.Ocr" Version="2.0.0" />
<PackageReference Include="Lyo.Pdf.Ocr" />
paket add Lyo.Pdf.Ocr --version 2.0.0
#r "nuget: Lyo.Pdf.Ocr, 2.0.0"
#:package Lyo.Pdf.Ocr@2.0.0
#addin nuget:?package=Lyo.Pdf.Ocr&version=2.0.0
#tool nuget:?package=Lyo.Pdf.Ocr&version=2.0.0
Lyo.Pdf.Ocr
PNG-renders a PDF page with Lyo.Pdf.Rendering (PDFium), runs an IOcrEngine from Lyo.Images.Ocr, then lifts OCR pixel boxes into PDF points.
Targets net10.0.
Examples
Add Pdf OCR in DI
services.AddTesseractOcrEngineFromConfiguration(configuration); // or any IOcrEngine
services.AddPdfOcr();
Read one page
public sealed class Worker(PdfOcrService pdfOcr, IPdfService pdfService)
{
public async Task<PdfOcrDocumentPage> RunAsync(byte[] pdfBytes, CancellationToken ct)
{
await using var reader = await pdfService.OpenFromBytesAsync(pdfBytes, ct);
var result = await pdfOcr.ReadPageAsync(reader, pageNumber1Based: 1, dpi: 200, cancellationToken: ct);
return result.ValueOrThrow();
}
}
ReadPageAsync pipeline
PdfOcrService has one method:
Task<Result<PdfOcrDocumentPage>> ReadPageAsync(
IPdfReader pdfReader,
int pageNumber1Based,
int dpi,
OcrReadRequest? ocrRequest = null,
string? pdfPassword = null,
CancellationToken cancellationToken = default);
Steps:
pdfReader.GetPageSizePoints(pageNumber1Based)for the PDF page dimensions.IPdfPageRasterizer.RenderPageToPngAsync(pdfReader.SourceBytes, …)for the pixel raster (and bitmap width/height).IOcrEngine.ReadAsync(pngStream, ocrRequest, …)for the per-word text and pixel-space bounding boxes (Y-up).OcrCoordinateTransforms.MapPixelBoxToPdfPoints(box, pageWidthPts, pageHeightPts, widthPx, heightPx)to lift eachOcrWordinto aPdfWord.
PdfOcrDocumentPage holds the original OcrPageResult, the projected
IReadOnlyList<PdfWord> WordsInPdfPoints, and the source page size.
Either stage can fail as Result<PdfOcrDocumentPage>.Failure;
unexpected exceptions are tagged with PdfOcrErrorCodes.ReadFailed
("PDF_OCR_READ_FAILED").
DI registration
AddPdfOcr adds PdfOcrService as a singleton and, if IPdfPageRasterizer is missing, calls AddPdfPageRasterizer. Register IOcrEngine yourself.
When OCR is worth it
Selectable-text PDFs should use IPdfReader.Text (PdfPig) instead. OCR is useful only when the PDF has no embedded text layer.
Dependencies
Generated from ProjectReference / PackageReference (same model as docs/Lyo.ProjectGraph.html).
Lyo.Exceptions(direct, lyo)Lyo.Images.Ocr(direct, lyo)Lyo.Pdf.Models(direct, lyo)Lyo.Pdf.Rendering(direct, lyo)Microsoft.Extensions.DependencyInjection.Abstractions10.0.5(direct, microsoft)Microsoft.Extensions.Logging.Abstractions10.0.5(direct, microsoft)Lyo.Common.Core(transitive, lyo)Lyo.Common.Metadata(transitive, lyo)Lyo.Configuration(transitive, lyo)Lyo.DataTable.Models(transitive, lyo)Lyo.Metrics(transitive, lyo)Lyo.Result(transitive, lyo)Microsoft.Bcl.AsyncInterfaces10.0.5(transitive, microsoft, netstandard2.0)Microsoft.Extensions.Configuration.Binder10.0.5(transitive, microsoft)PDFtoImage5.2.1(transitive, third-party)SixLabors.ImageSharp3.1.12(transitive, third-party)System.Memory4.6.3(transitive, microsoft, netstandard2.0)System.Text.Json10.0.5(transitive, microsoft, netstandard2.0)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Lyo.Exceptions (>= 2.0.0)
- Lyo.Images.Ocr (>= 2.0.0)
- Lyo.Pdf.Models (>= 2.0.0)
- Lyo.Pdf.Rendering (>= 2.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.