Proposal: ONNX layout detection + .NET data ingestion bridge for PdfPig #1285
Replies: 2 comments 1 reply
|
@luisquintanilla thanks for posting the above, everything you said makes a lot of sense and is in line with my original idea when I started working on the Document Layout Analysis part. This is really exciting and from my point of view, choosing Onnx makes complete sense, and Data Ingestion is critical. I believe the best approach to implement that is for you to create your own repos. This is what I did when developing https://github.com/BobLd/PdfPig.Rendering.Skia (and dependencies). This will give you full control over your code, progress and how you ship the packages. Where PdfPig can help is by making implementing your projects doable (making classes and constructors plublic, adding new interfaces, etc.) Other people have started looking into it, see BobLd/RapidOcrNet#13 I would really like to update PdfPig so that people can easily integrate it with new models and data ingestion pipelines - keep me posted |
Uh oh!
There was an error while loading. Please reload this page.
Summary
.NET developers building AI applications need a way to go from PDF to structured, AI-ready content. Python has Docling and PyMuPDF. Node has tools like LiteParse. .NET has few if any equivalents in the space, and PdfPig provides a good foundation to build one on.
I prototyped two new packages on a fork (
feature/intelligent-pdf-ingestionbranch) that extend PdfPig in two directions:UglyToad.PdfPig.DocumentLayoutAnalysis.Onnx: ML-powered layout detection via ONNX, implemented as a drop-inIPageSegmenterUglyToad.PdfPig.DataIngestion: Connects PdfPig to the .NET data ingestion building blocks (Microsoft.Extensions.DataIngestion), so PdfPig becomes a first-class document reader in .NET AI pipelinesI'd love to get your thoughts on whether you'd be open to accepting either or both upstream before I clean them up into formal PRs.
Package 1:
DocumentLayoutAnalysis.OnnxThis one extends PdfPig's existing
IPageSegmenterinterface with ONNX model support. Same input (Wordobjects), same output (TextBlockresults). Instead of heuristics, it runs an ONNX layout detection model to identify regions.How it works:
Wordbounding boxes as a page image (filled rectangles on white background, which is the format layout detection models expect)Wordobjects via bounding box overlapTextBlockresults, same asDocstrumBoundingBoxesorRecursiveXYCutHere's what the API looks like:
That's it. Same
IPageSegmenterinterface, sameGetBlockscall.New types:
OnnxPageSegmenterIPageSegmenterimplementation. Drop-in replacement for existing segmenters.ILayoutDetectionModelRtDetrLayoutModelConfigurableLayoutModelImagePreprocessingDetectionPostprocessingLayoutDetectionBoundingBox(PdfRectangle),Label,ClassId,ConfidencePageImageRendererWordbounding boxes as filled rectangles for model inputOnnxSegmenterOptionsSessionOptions, render DPIDependencies: ONNX Runtime, SkiaSharp (for page rendering). These are only pulled in by users who reference this package. No impact on the core PdfPig package.
Package 2:
DataIngestion.NET recently shipped data ingestion building blocks (
Microsoft.Extensions.DataIngestion). These are a set of abstractions for composing AI data pipelines: read documents → process/enrich → chunk → embed → store. The pipeline is built around a few key interfaces:IngestionDocumentReader: reads files into a structuredIngestionDocumentIngestionDocumentProcessor: transforms/enriches documents before chunkingIngestionChunkProcessor<T>: enriches individual chunks after splittingIngestionPipeline<T>: chains it all togetherThe
DataIngestionpackage implements these interfaces for PdfPig. It provides:PdfPigReaderIngestionDocumentReaderimplementation. Reads PDFs intoIngestionDocumentwith per-page sections. Supports three reading modes: TextOnly (native PdfPig extraction), Hybrid (text + page images), and VisionOnly (renders every page as PNG for vision LLM processing). Delegates segmentation to anyIPageSegmenter.VisionOcrEnricherIngestionDocumentProcessorthat sends page images to a vision LLM viaIChatClientto extract text. Makes VisionOnly mode work. Useful for scanned documents and image-heavy PDFs.VisionTableEnricherIngestionDocumentProcessorthat sends table regions to a vision LLM and gets back markdown tables.ContextualChunkEnricherIngestionChunkProcessor<string>that generates contextual summaries per chunk viaIChatClientfor better retrieval.PageImageRendererWhy this matters for PdfPig: it means a developer can go from
PdfDocument.Openall the way to a vector store with a composable pipeline. PdfPig becomes a first-class reader in the .NET AI data ingestion ecosystem, similar to the built-in MarkdownReader and MarkItDown readers.Here's a full pipeline using both packages together:
The
OnnxPageSegmenterhandles layout detection. ThePdfPigReaderturns that intoIngestionDocumentsections. TheIngestionPipelineenriches, chunks, and writes everything to a vector store. Each piece is swappable.Dependencies:
Microsoft.Extensions.DataIngestion,Microsoft.Extensions.AI, SkiaSharp. Again, separate package. No impact on core PdfPig.A sample app (PdfAIngest) shows the full end-to-end: Blazor UI + PdfPig in VisionOnly mode + MEDI pipeline + Qdrant vector search.
Design decisions
A few things that might be relevant for your review:
IPageSegmenteris the right interface. The ONNX segmenter extends your existing architecture. Same input, same output. PdfPig already does the hard work of getting accurateWordpositions. The ONNX model just adds region classification on top.ILayoutDetectionModelas a strategy. Model-specific logic (tensor format, output parsing) is encapsulated behind an interface. The segmenter itself is model-agnostic. Ships withRtDetrLayoutModel(17 element types) andConfigurableLayoutModel(bring your own).Word-level spatial mapping. The ONNX model detects WHERE layout regions are; PdfPig tells us WHAT text is in those regions. Mapping uses bounding box overlap. Works well because PdfPig'sWordobjects already have accurate spatial coordinates.DataIngestionpackage pulls inMicrosoft.Extensions.AIandMicrosoft.Extensions.DataIngestion. Core PdfPig stays clean.Questions
Before I invest in polishing these into PRs, a few things I'd like to run by you:
DocumentLayoutAnalysispackage) and follow up withDataIngestion, or do both together.Thanks for PdfPig. The
IPageSegmenterdesign made this a natural extension to build. Looking forward to hearing your thoughts.All reactions