Structured extraction
Extract tables, label/value pairs and typed entities from the document shown in a .NET MAUI PdfView, build prompts for your own language model, validate the answer and classify documents.
License module
Structured extraction needs the conversion module (Startup tier and above).
Everything on this page runs on the device, offline, and gives the same result every time. For
schema extraction and classification, NexPDF builds the prompt and checks the answer; the engine
never calls a network service, so you choose and call the language model yourself. Run these
calls with Task.Run: they read every page.
Tables
ExtractTablesJson(pageIndex, optionsJson) finds ruled and unruled tables. Pass -1 for every
page, or view.CurrentPage for the page on screen:
using System.Text.Json.Nodes;
var doc = view.Document!;
var tables = JsonNode.Parse(await Task.Run(() => doc.ExtractTablesJson(view.CurrentPage)))!["tables"]!.AsArray();
foreach (var t in tables)
System.Diagnostics.Debug.WriteLine($"{t!["kind"]} {t["n_rows"]} x {t["n_cols"]}\n{t["markdown"]}");Each table has page, bbox, kind (ruled or unruled), n_rows, n_cols, header_rows,
rows (cells with text, bbox, row, col, row_span, col_span), csv and markdown.
Key-value pairs and entities
ExtractKeyValuesJson(optionsJson) reads label/value pairs from the layout and typed entities
(dates, amounts with currency, IBANs, e-mail addresses, phone numbers and VAT numbers) with
normalised values:
var kv = JsonNode.Parse(await Task.Run(() => doc.ExtractKeyValuesJson("""{"date_order":"dmy"}""")))!;
foreach (var p in kv["pairs"]!.AsArray())
System.Diagnostics.Debug.WriteLine($"{p!["key"]} = {p["value"]} ({p["type"]})");Schema extraction with your language model
ExtractionPromptJson(schemaJson, optionsJson)builds a prompt from the page text, the tables and the key-values.- Send
systemandpromptto the model of your choice. ExtractionValidateJson(schemaJson, answer)checks and coerces the answer and finds each value on the pages.
var schema = """{"invoice_number":"string","date":"date","total":"number"}""";
var p = JsonNode.Parse(await Task.Run(() => doc.ExtractionPromptJson(schema)))!;
var answer = await myLlm.CompleteAsync((string)p["system"]!, (string)p["prompt"]!); // your call
var result = JsonNode.Parse(doc.ExtractionValidateJson(schema, answer))!;
if (result["valid"]!.GetValue<bool>())
TotalLabel.Text = result["value"]!["total"]!.ToString();The fields of the result give page and bbox for each value found, so you can move the view
there with GoToPageAsync. Schemas, prompt options and the report are described in the
.NET structured extraction guide.
Classification
ClassifyDocumentJson(labelsJson) picks one of your labels from keywords:
var c = JsonNode.Parse(await Task.Run(() => doc.ClassifyDocumentJson("""["invoice","contract","receipt"]""")))!;
KindLabel.Text = c["label"]?.ToString() ?? "unknown";The result also has scores, and system, prompt and response_schema for a language model.