TsgcAIDocumentExtractor

Extracts structured data from PDF documents and images with any AI provider.

Introduction

TsgcAIDocumentExtractor reads PDF documents and images (invoices, receipts, identity documents or any document you describe with a JSON Schema) and returns the data they contain as a JSON document. It sends the document to an AI model through a linked TsgcAIChat, so every provider of TsgcAIChat can be used, and it checks the answer locally before returning it.

 

How It Works

 

Supported Documents

PDF, PNG, JPEG, GIF and WEBP files. PDF input depends on the provider selected in the linked TsgcAIChat:

 

ProviderPDFImages
aicpOpenAIYesYes
aicpAnthropicYesYes
aicpGeminiYesYes
aicpDeepSeekNoYes
aicpGrokNoYes
aicpMistralNoYes
aicpOllamaNoYes

 

Schema Rules

With OpenAI the schema is sent in strict mode: every object must declare "additionalProperties": false, every property must be listed in required, and optional values use a nullable type such as ["string","null"]. The presets already follow these rules. The local validation checks type, properties, required, items and enum, recursing into nested objects and arrays.

 

Local OCR

Link a TsgcAIOCRTesseract or a TsgcAIOCRWindows to OCR and set OCRMode to recognise the text of the images locally, either as a fallback when the vision request fails (domFallback) or always (domAlways), which lets a text only model extract the data. The local OCR accepts images only.

Two local OCR engines are available. TsgcAIOCRTesseract runs the Tesseract command line tool on Windows, Linux and macOS, and needs Tesseract installed. TsgcAIOCRWindows uses the OCR engine built into Windows 10 and later, so nothing has to be installed or deployed; it reads the OCR language packs installed in Windows.

 

Quick Start

oChat.Provider := aicpOpenAI;
oChat.ChatOptions.ApiKey := 'API_KEY';
oChat.ChatOptions.Model := 'gpt-4o-mini';

oExtractor.AIChat := oChat;
oExtractor.Preset := dpInvoice;
vJSON := oExtractor.ExtractFile('invoice.pdf');

 

The step by step guide Extract data from an invoice shows a complete example, with confidence checks and the OCR fallback.

 

Reference

See Also