Skip to content

Add Tabula table extraction to the PdfPig provider - #99

Open
luisquintanilla wants to merge 3 commits into
mainfrom
luisquintanilla-pdfpig-tabula-tables
Open

luisquintanilla wants to merge 3 commits into
mainfrom
luisquintanilla-pdfpig-tabula-tables

Conversation

@luisquintanilla

Copy link
Copy Markdown
Contributor

Closes #98

Summary

CommunityToolkit.DocumentExtraction.PdfPig now detects tables in native (searchable) PDFs using tabula-sharp (Tabula 1.0.1) and emits them as ExtractionDocumentTableElements with cell text, row/column spans and source polygons. Words placed in a table cell are removed from the surrounding text blocks, so content is not duplicated.

API

  • New PdfPigDocumentExtractionClientOptions (keeps future settings off the constructor):
    • TableExtraction (PdfPigTableExtractionMode)
    • PageSegmenter, WordExtractor, RoleResolver (previously constructor-only)
  • New PdfPigTableExtractionMode:
    • None: text only (previous behavior)
    • Lattice (default): tables drawn with ruling lines
    • LatticeThenStream: lattice, then whitespace-aligned tables on the remaining text
  • The existing constructor is preserved and delegates to the options constructor.
  • Package version bumped to 1.1.0-preview.

Behavior change

With the new Lattice default, words inside ruled tables now appear as table cells instead of text blocks. Set TableExtraction = PdfPigTableExtractionMode.None to get the earlier output; a corpus test verifies it is byte-identical to the previous behavior.

Pipeline notes / deviations from the original issue sketch

  • Cell text is built from PdfPig words (not Tabula's text chunks), which keeps spacing and reading order consistent with text blocks.
  • Merged cells come from Tabula's lattice geometry. If spans can't be inferred consistently, the table is rejected rather than emitted with wrong structure.
  • Nested cells are dropped. A detected table must have text in at least 2 rows and 2 columns, which filters out boxes and rules.
  • Text blocks are banded around detected tables so a block never spans across a table and reading order is preserved.

Tests

  • Unit tests (PdfPigTableExtractionTests): synthetic PDFs cover lattice, merged cells, stream mode, None, word conservation, options validation and the legacy constructor.
  • Corpus regression tests (PdfPigCorpusTests) on 15 pages from the public ParseBench-2 dataset (Apache-2.0), pinned to a specific revision:
    • 10 hard table pages (T1–T10) and 5 negative controls (N1–N5);
    • checks dimensions per mode, cell facts, spans, word conservation, absence of false positives on controls, and None parity.
  • PDFs are not committed. test/Corpus/ParseBench-2/corpus.json lists URLs and SHA-256 hashes:
    • files are downloaded at test time, hash-verified and cached (DOCUMENT_EXTRACTION_CORPUS_DIR or the temp directory);
    • 429/5xx responses are retried;
    • CI caches the corpus with actions/cache, keyed on the manifest hash;
    • offline tests cover the cache's hash validation and stale-file replacement.

Results: all DocumentExtraction test projects pass in Release (PdfPig: 193 tests). dotnet pack produces a nuspec with dependencies on PdfPig 0.1.15 and Tabula 1.0.1.

Corpus evidence (lattice/stream adjacency agreement against ParseBench-2 ground truth)

Page Agreement
T1 4/48
T2 60/260
T3 344/612
T4 48/130
T5 88/143
T6 126/200
T7 26/169
T8 112/117
T9 75/112
T10 754/1076

Word conservation: no missing or extra characters on any page. Typical page time is under 250 ms; the densest page takes about 1 s.

Known limitations

These are documented in the README and in corpus.json known gaps.

  • Lattice finds only ruled tables. On partially ruled tables it may capture just the ruled header.
  • LatticeThenStream can report charts or forms as tables and does not infer merged cells.
  • No OCR or image-based table recognition (scanned PDFs are unchanged).

Docs

Updated the PdfPig README (new "Read tables" section and an options example), the DocumentExtraction and root READMEs, and the PdfPig agent skill.

Luis Quintanilla and others added 3 commits November 25, 2026 16:46
Introduce PdfPigDocumentExtractionClientOptions and PdfPigTableExtractionMode.
Lattice (default) detects ruled tables with merged-cell spans; LatticeThenStream
adds whitespace-aligned tables. Cell text is built from PdfPig words so every
word is emitted exactly once, and None preserves the previous output.

Refs #98

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Pins 10 table pages and 5 negative controls from llamaindex/ParseBench-2 by revision and SHA-256. The tests download and cache the pages, and check text conservation, None-mode compatibility, table dimensions, reference facts and well-formed grids. Refs #98

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Refs #98

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DocumentExtraction] Incorporate tabula-sharp to support tables for the PdfPig based extraction

1 participant