Skip to Content
NexPDF

Text extraction

Extract Markdown, page text and positioned text blocks with NexPDF for Flutter, and retrieve passages for a language model.

English

License module

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

The whole document as Markdown

toMarkdown() keeps headings, lists and tables, which makes it a good input for search indexes and language models:

final markdown = doc.toMarkdown();

Passages for a language model

ask(question, k:, maxChars:) ranks the passages of the document against a question, offline, and returns the k best with a page-tagged context you can send to a language model. maxChars: 0 (the default) uses the default limit of 200,000 characters.

final result = doc.ask('When does the contract end?', k: 5);
for (final p in (result['passages'] as List).cast<Map<String, Object?>>()) {
  print('${p['page']} ${p['score']} ${p['text']}');
}
final prompt = 'Answer from this document:\n${result['context']}\n\nQuestion: When does the contract end?';

Plain text of a page

nexpdf_extract_page_text returns the text of a page in reading order, with the layout reconstructed into lines and paragraphs. It has no Dart method; call it through dart:ffi:

import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'nexpdf_ffi.dart';

// char *nexpdf_extract_page_text(const NexPdfDocument *doc, size_t page_index);
final extractPageText = nexpdfLib.lookupFunction<
    Pointer<Utf8> Function(Pointer<Void>, Size),
    Pointer<Utf8> Function(Pointer<Void>, int)>('nexpdf_extract_page_text');

String pageText(NexPdfDocument doc, int page) =>
    nexpdfCall(() => takeString(extractPageText(doc.handle, page))) ?? '';

final text = [for (var i = 0; i < doc.pageCount; i++) pageText(doc, i)].join('\n\n');

Text blocks with positions

nexpdf_page_blocks_json(doc, page_index) has the same signature and returns the text blocks of a page as JSON, each with text, size, llx, lly, urx and ury in PDF points:

import 'dart:convert';

final pageBlocks = nexpdfLib.lookupFunction<
    Pointer<Utf8> Function(Pointer<Void>, Size),
    Pointer<Utf8> Function(Pointer<Void>, int)>('nexpdf_page_blocks_json');

final blocks = jsonDecode(takeString(pageBlocks(doc.handle, 0)) ?? '[]') as List;

Words with exact boxes

nexpdf_page_words_json(doc, page_index, options_json) returns the words of a page with boxes computed from the fonts' glyph widths, for highlighting or redacting single words:

final pageWords = nexpdfLib.lookupFunction<
    Pointer<Utf8> Function(Pointer<Void>, Size, Pointer<Utf8>),
    Pointer<Utf8> Function(Pointer<Void>, int, Pointer<Utf8>)>('nexpdf_page_words_json');

final page = jsonDecode(takeString(pageWords(doc.handle, 0, nullptr)) ?? '{}') as Map<String, Object?>;
for (final w in (page['words'] as List).cast<Map<String, Object?>>()) {
  print('${w['text']} ${w['bbox']} ${w['line']}');
}

The result is {"page", "words": [...]}. Each word has text, bbox ([llx, lly, urx, ury]), quad (eight numbers: baseline start, baseline end, top end, top start, so it follows rotated text), font, size, angle and line. With {"glyphs": true} each word also has glyphs, one box per character (text, bbox, quad, code).

Scanned documents

Pages that are images have no text to extract. Run OCR first to add a searchable text layer.

On this page