Skip to Content
NexPDF

Text extraction

Extract page text, Markdown and passages for an on-device assistant from the document shown in a .NET MAUI PdfView.

English

License module

Text extraction, Markdown export and the offline assistant are part of the viewer module and work without a key.

view.Document gives the full text API of the engine. Run it off the main thread so the UI stays responsive.

Text of the current page

var doc = view.Document!;
var text = await Task.Run(() => doc.ExtractPageText(view.CurrentPage));
await Clipboard.Default.SetTextAsync(text);

The whole document as Markdown

var markdown = await Task.Run(() => doc.ToMarkdown());

Text blocks with positions

PageBlocksJson(page) returns blocks with text, size, llx, lly, urx and ury in PDF points.

PageWordsJson(page, optionsJson) returns the words with exact boxes from the fonts' glyph widths (text, bbox, quad, font, size, angle, line), handy to highlight a word over the view; {"glyphs":true} adds glyph boxes:

var words = JsonNode.Parse(await Task.Run(() => doc.PageWordsJson(view.CurrentPage)))!["words"]!.AsArray();

An on-device document assistant

AskDocumentJson(question, k, maxChars) ranks the passages of the document against a question, offline, and returns the k best with a page-tagged context for a language model:

using System.Text.Json.Nodes;

var result = JsonNode.Parse(await Task.Run(() => doc.AskDocumentJson("When does the contract end?")))!;
var context = result["context"]!.GetValue<string>();
var firstPage = result["passages"]![0]!["page"]!.GetValue<int>();
await view.GoToPageAsync(firstPage);

See the .NET text extraction guide for the full details. Scanned pages need OCR first.

On this page