Layout Detection
Detect document layout regions (tables, figures, headers, text blocks, etc.) in PDFs using ONNX-based deep learning models. Enables table extraction, figure isolation, reading-order reconstruction, and selective OCR.
See the LayoutDetectionConfig reference for all configuration options.
Layout detection uses the RT-DETR v2 model, an ONNX-based deep learning model that detects document layout regions. Regions are labeled with one of 18 LayoutClass categories: text blocks, tables, figures, charts, headers, footers, captions, code, lists, sections, formulas, footnotes, page headers/footers, titles, checkboxes, key-value regions, and document indices.
Layout detection now populates ExtractedDocument.formulas for formula regions and supports chart understanding via
enable_chart_understanding.
When to Enable
Section titled “When to Enable”Recommended for: complex multi-column PDFs, scanned documents, academic papers, business forms, and any document where layout understanding improves extraction accuracy.
Less beneficial for: simple single-column text documents, high-throughput pipelines where latency is critical (consider GPU acceleration), or documents already well-handled by PDF structure trees.
Performance Impact
Section titled “Performance Impact”| Pipeline | Structure F1 | Text F1 | Avg time/doc |
|---|---|---|---|
| Baseline | 33.9% | 87.4% | 447 ms |
| Layout | 41.1% | 90.1% | 1500 ms |
171-document PDF corpus, CPU only. GPU acceleration significantly reduces the time penalty.
Configuration
Section titled “Configuration”from xberg import ExtractInput, ExtractionConfig, LayoutDetectionConfig, extract
config = ExtractionConfig( layout=LayoutDetectionConfig( confidence_threshold=0.5, apply_heuristics=True, table_model="tatr", ))output = await extract(ExtractInput(kind="uri", uri="document.pdf"), config=config)import { ExtractInputKind, extract } from "@xberg-io/xberg";
const output = await extract({ kind: ExtractInputKind.Uri, uri: "document.pdf",}, { layout: { confidenceThreshold: 0.5, applyHeuristics: true, tableModel: "tatr", },});use xberg::core::{ExtractionConfig, LayoutDetectionConfig};
let config = ExtractionConfig { layout: Some(LayoutDetectionConfig { confidence_threshold: Some(0.5), apply_heuristics: true, table_model: Some("tatr".to_string()), ..Default::default() }), ..Default::default()};[layout]apply_heuristics = true# table_model = "tatr"# Enable layout detection with default settingsxberg extract document.pdf --layout --content-format markdown
# Custom confidence thresholdxberg extract document.pdf --layout-confidence 0.5 --content-format markdown
# Specific table modelxberg extract document.pdf --layout --layout-table-model slanet_wired
# Adaptive page selection: run the model only on pages that can benefitxberg extract document.pdf --layout --layout-strategy auto
# Combined with GPU accelerationxberg extract document.pdf --layout --acceleration coremlSee LayoutDetectionConfig for all fields.
Layout-Informed Markdown
Section titled “Layout-Informed Markdown”use_layout_for_markdown feeds detected regions into the non-OCR PDF markdown pipeline to drive heading, table, list, and figure detection that would otherwise rely on font-clustering heuristics alone. It is a top-level ExtractionConfig field — set it alongside layout, not inside LayoutDetectionConfig.
Enabling it improves structural F1 at the cost of inference latency (~150-300 ms/page CPU, ~20-50 ms/page GPU). Default: false. Requires the layout-detection feature and a set layout config; it is skipped when force_ocr is enabled. On the CLI, pass --use-layout-for-markdown.
Page Selection Strategy
Section titled “Page Selection Strategy”LayoutDetectionConfig.strategy controls which pages the layout model runs on:
always(default): every page is rendered and inferred, the historical behavior.auto: a cheap per-page pre-screen runs first, and the model only processes pages likely to benefit: multi-column text, table geometry, ruled grids, heavy graphics, form widgets, sparse or absent text layers, or pages whose signals could not be gathered.
The pre-screen reads geometry the PDF already carries (text-span boxes, straight lines, image and path bounding boxes, annotations) without decoding any pixels, so it costs a small fraction of one model pass. It is recall-biased: an ambiguous page always runs the model. A skipped page is processed exactly like a page where the model ran and found no regions, and on plain single-column prose that output is identical at a fraction of the cost.
Two behaviors to know:
- On the OCR path the pre-screen skips inference only; page rasters are still produced because OCR consumes them.
- Skipped pages are auditable:
metadata.format.layout_gated_pageslists them (1-indexed), andmetadata.format.layout_gate_reasonsrecords each page’s decision reason (multi_column,table_grid,plain_text, and so on). Theformatobject is taggedformat_type: "pdf".
Note that auto trades the model’s per-paragraph refinements on skipped pages (heading promotion, code and formula tagging) for throughput; content and reading order are unchanged. Keep always when you need the model’s classification on every page.
Table Structure Models
Section titled “Table Structure Models”When layout detection identifies a table region, a table structure model analyzes rows, columns, headers, and spanning cells. Set LayoutDetectionConfig.table_model to one of:
| Value | Notes |
|---|---|
tatr |
Default. Fast (~30 MB). General-purpose. |
slanet_wired |
Higher accuracy for bordered/gridlined tables (~365 MB). |
slanet_wireless |
Higher accuracy for borderless tables (~365 MB). |
slanet_auto |
Auto-classifies per page (~737 MB). Slowest. |
slanet_plus |
Smallest (~7.78 MB). For resource-constrained environments. |
disabled |
Skip table structure recognition. |
GPU Acceleration
Section titled “GPU Acceleration”Layout detection uses ONNX Runtime with automatic provider selection:
| Provider | Platform | Notes |
|---|---|---|
| CPU | All | Default, no setup needed |
| CUDA | Linux, Windows | Requires CUDA toolkit + cuDNN |
| CoreML | macOS | Automatic on Apple Silicon |
| TensorRT | Linux | Requires TensorRT |
To override:
config = ExtractionConfig( layout=LayoutDetectionConfig(), acceleration=AccelerationConfig(provider="cuda", device_id=0))See AccelerationConfig reference for details.
Layout Classes
Section titled “Layout Classes”The LayoutClass taxonomy defines 18 classes. Each LayoutRegion.class_name is one of:
caption, chart, footnote, formula, list_item, page_footer, page_header, picture, section_header, table, text, title, document_index, code, checkbox_selected, checkbox_unselected, form, key_value_region.
See LayoutRegion in the types reference for the full field shape.
Accessing Layout Regions
Section titled “Accessing Layout Regions”When layout detection is enabled AND page extraction is enabled, each page in the result includes layout_regions — a list of detected regions with class, confidence score, bounding box, and area fraction. This enables programmatic filtering and analysis of specific layout elements.
from xberg import ExtractInput, extract, ExtractionConfig, LayoutDetectionConfig, PagesConfig
output = await extract( ExtractInput(kind="uri", uri="document.pdf"), config=ExtractionConfig( layout=LayoutDetectionConfig(), pages=PagesConfig(extract_pages=True), ),)result = output.results[0]
for page in result.pages: if page.layout_regions: for region in page.layout_regions: if region.class_name == "picture" and region.confidence > 0.9: print(f"Page {page.page_number}: diagram detected " f"(confidence={region.confidence:.2f}, " f"area={region.area_fraction:.0%})")import { ExtractInputKind, extract } from "@xberg-io/xberg";
const output = await extract({ kind: ExtractInputKind.Uri, uri: "document.pdf",}, { layout: {}, pages: { extractPages: true },});const result = output.results[0];
for (const page of result.pages ?? []) { if (page.layoutRegions) { for (const region of page.layoutRegions) { if (region.className === "picture" && region.confidence > 0.9) { console.log( `Page ${page.pageNumber}: diagram detected ` + `(confidence=${region.confidence.toFixed(2)}, ` + `area=${(region.areaFraction * 100).toFixed(0)}%)` ); } } }}use xberg::{extract, ExtractInput, ExtractionConfig, LayoutDetectionConfig, PagesConfig};
let config = ExtractionConfig { layout: Some(LayoutDetectionConfig::default()), pages: Some(PagesConfig { extract_pages: true, ..Default::default() }), ..Default::default()};
let output = extract( ExtractInput::from_uri("document.pdf"), &config,).await?;let result = &output.results[0];
for page in &result.pages { if let Some(regions) = &page.layout_regions { for region in regions { if region.class_name == "picture" && region.confidence > 0.9 { println!( "Page {}: diagram detected (confidence={:.2}, area={:.0}%)", page.page_number, region.confidence, region.area_fraction * 100.0 ); } } }}- Use
confidenceto filter low-confidence detections — typically ≥ 0.8–0.9 for downstream operations - Use
area_fractionto distinguish between inline images and full-page diagrams (e.g.,area_fraction > 0.1for significant figures) - Regions are independent of page extraction — enable both to access both content and layout structure
- Available across all bindings (Python, TypeScript, Rust, Ruby, Java, Go, Elixir, C#, PHP)
Acknowledgments
Section titled “Acknowledgments”- Docling — RT-DETR v2 model and layout classification approach
- TATR — Table structure recognition with ONNX
- PaddleOCR — SLANeXT table structure and PP-LCNet classifier models
Related
Section titled “Related”- Configuration Reference — full field reference
- Element-Based Output — using layout-aware results