Skip to Content
NexPDF

API reference

Every class and method of the NexPDF Flutter plugin, and how to call any function of the NexPDF C API from Dart with dart:ffi.

English

Everything below is exported by package:nexpdf_flutter/nexpdf_flutter.dart. The plugin is version 1.0.0.

NexPdfFlutter

Loads the engine, activates the license and opens documents.

NexPdfFlutter([DynamicLibrary? library])
static String get platform
void activateLicense(String license, {required String appId, String? platform})
String? lastError()
NexPdfDocument open(Uint8List bytes, {String? password})
Future<NexPdfDocument> openFile(String path, {String? password})
Member
NexPdfFlutter([library])Loads the engine: the process on iOS, libnexpdf_ffi.dylib on macOS, nexpdf_ffi.dll on Windows, libnexpdf_ffi.so on Android and Linux, or the DynamicLibrary you pass
platformThe license platform of this device: ios, android, macos, windows or linux
activateLicenseActivates a key or license XML for appId. Throws NexPdfException if the result is not 1; see the note in License
lastError()The message of the last failed call on this thread, or null
openOpens PDF bytes, or JPEG, PNG and TIFF pictures (one page each). Throws NexPdfException, for example password required or incorrect password
openFileReads a file and opens it

The raw-handle methods pageCount(Pointer<Void>), renderPagePng(Pointer<Void>, int, {dpi}) and disposeDocument(Pointer<Void>) are kept for code written against version 0.1.0.

NexPdfDocument

An open document. Not thread-safe: use it from one isolate.

Pointer<Void> get handle
bool get isDisposed
int get pageCount
({double width, double height}) pageSize(int pageIndex)
NexPdfPageView pageView(int pageIndex)
Uint8List renderPagePng(int pageIndex, {double dpi = 150.0})
Uint8List save()
String toMarkdown()
List<NexPdfSearchHit> search(String query)
Map<String, Object?> ask(String question, {int k = 5, int maxChars = 0})
void dispose()
MemberModule
handleThe native NexPdfDocument*, for C calls. Throws StateError after dispose()
pageCountviewerNumber of pages
pageSizeviewerDisplayed size in points: crop box, /Rotate and /UserUnit applied
pageViewviewerThe display geometry and transforms of a page
renderPagePngviewerPNG bytes at dpi. Throws NexPdfException on failure
saveviewerThe document with every change as PDF bytes
toMarkdownviewerThe text as Markdown: headings, lists and tables
searchviewerCase-insensitive search in reading order
askviewerOffline retrieval: the k best passages and a page-tagged context for a language model
disposeFrees the document. A finalizer frees it otherwise

NexPdfPageView

How a page is displayed and the transform between PDF user space and display coordinates (origin at the top-left of the page as shown, y down).

Member
width, heightDisplayed size in points
rotation, userUnit/Rotate and /UserUnit
cropThe crop box (llx, lly, urx, ury) in PDF user space
toDisplayThe matrix [a, b, c, d, e, f] from PDF user space to display points
toView(x, y, [scale])A PDF point to display pixels at scale pixels per point
toPdf(vx, vy, [scale])Display pixels to a PDF point, for example a tap
deltaToPdf(dx, dy, [scale])A displacement on screen as a displacement in PDF space
rectToView(llx, lly, urx, ury, [scale])A PDF rectangle to (left, top, right, bottom) pixels
rectToPdf(x1, y1, x2, y2, [scale])Two display corners to a PDF (llx, lly, urx, ury)
NexPdfPageView.plain(width, height)An unrotated page with its origin at 0,0
NexPdfPageView.fromJson(json)From the JSON of nexpdf_page_view_json

NexPdfSearchHit

page (0-based), context (the text around the match) and llx, lly, urx, ury (the rectangle in PDF points).

NexPdfException

Thrown when a NexPDF call fails. message is the engine's error.

NexPdfReader

The native reader of the iOS and Android SDKs. See Reader.

static Stream<NexPdfReaderEvent> get events
static Future<void> open({Uint8List? document, String fileName = 'document.pdf', NexPdfUIConfig config = const NexPdfUIConfig()})
static Future<void> close()

NexPdfReaderEvent has type and data (Map<String, Object?>).

NexPdfViewerWidget

const NexPdfViewerWidget({Key? key, required NexPdfDocument document, NexPdfUIConfig config = const NexPdfUIConfig(), NexPdfEventCallback? onEvent})
typedef NexPdfEventCallback = void Function(String type, Map<String, Object?> data);

A pure-Flutter viewer. It does not dispose document.

NexPdfUIConfig

The shared UI configuration.

Member
NexPdfUIConfig([json]), NexPdfUIConfig.fromJson(map)From a JSON map
NexPdfUIConfig.parse(source)From JSON text; invalid JSON gives the defaults
jsonThe raw map, passed as is to the native readers
toJsonString()The configuration as JSON text
readOnly, initialMode, availableModes, modeAllowed(mode)Mode settings
hiddenItems, hidden(id), customItems, customItemsAt(location, {mode})Toolbar settings
annotationTools, formTools, annotationToolAllowed(id), formToolAllowed(id)Tool lists
defaultColor, colorPalette, stampsAnnotation defaults
themeMode, accentColor, language, string(key, language)Theme and language
NexPdfUIConfig.parseColor(hex)#RRGGBB or #RRGGBBAA to a Color

NexPdfCustomItem is a custom button: id, title, icon, location (top, toolbar or more, the default) and modes.

Calling the C API

Every other engine function is a C function in nexpdf.h, which ships in NexPdfCore.xcframework and in the include/ folder of the nexpdf-ffi archives. Call it with dart:ffi on doc.handle. Put these helpers in your app once:

lib/nexpdf_ffi.dart
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';

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

/// The engine library, loaded the same way NexPdfFlutter loads it.
final DynamicLibrary nexpdfLib = Platform.isIOS
    ? DynamicLibrary.process()
    : Platform.isMacOS
        ? DynamicLibrary.open('libnexpdf_ffi.dylib')
        : Platform.isWindows
            ? DynamicLibrary.open('nexpdf_ffi.dll')
            : DynamicLibrary.open('libnexpdf_ffi.so');

final _clearError = nexpdfLib.lookupFunction<Void Function(), void Function()>('nexpdf_clear_error');
final _lastError = nexpdfLib.lookupFunction<Pointer<Utf8> Function(), Pointer<Utf8> Function()>('nexpdf_last_error');
final _freeString = nexpdfLib.lookupFunction<Void Function(Pointer<Utf8>), void Function(Pointer<Utf8>)>('nexpdf_free_string');
final _freeBytes = nexpdfLib.lookupFunction<Void Function(NexPdfBytes), void Function(NexPdfBytes)>('nexpdf_free_bytes');

/// Copies and frees a string returned by the engine. null: the call failed.
String? takeString(Pointer<Utf8> p) {
  if (p == nullptr) return null;
  try {
    return p.toDartString();
  } finally {
    _freeString(p);
  }
}

/// Copies and frees a byte buffer returned by the engine.
Uint8List takeBytes(NexPdfBytes b) {
  if (b.ptr == nullptr || b.len == 0) return Uint8List(0);
  final copy = Uint8List.fromList(b.ptr.asTypedList(b.len));
  _freeBytes(b);
  return copy;
}

/// Copies Dart bytes into native memory owned by [arena].
Pointer<Uint8> toNative(Uint8List bytes, Arena arena) {
  final p = arena<Uint8>(bytes.length);
  p.asTypedList(bytes.length).setAll(0, bytes);
  return p;
}

/// Runs one engine call and throws NexPdfException if it failed.
T nexpdfCall<T>(T Function() call) {
  _clearError();
  final result = call();
  final error = takeString(_lastError());
  if (error != null) throw NexPdfException(error);
  return result;
}

Then look up a function with its C signature and call it:

// int32_t nexpdf_fill_form_field(NexPdfDocument *doc, const char *name, const char *value);
final fillFormField = nexpdfLib.lookupFunction<
    Int32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>),
    int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>)>('nexpdf_fill_form_field');

using((a) => nexpdfCall(() => fillFormField(
    doc.handle, 'applicant.name'.toNativeUtf8(allocator: a), 'Ana Silva'.toNativeUtf8(allocator: a))));
C typeNative typeDart type
NexPdfDocument *Pointer<Void>Pointer<Void>
const char *Pointer<Utf8>Pointer<Utf8>
char * (returned)Pointer<Utf8>Pointer<Utf8>, then takeString
NexPdfBytes (returned)NexPdfBytesNexPdfBytes, then takeBytes
const uint8_t *, size_t lenPointer<Uint8>, SizePointer<Uint8>, int
size_tSizeint
int32_t, uint32_tInt32, Uint32int
doubleDoubledouble

Functions that return a new NexPdfDocument * (such as nexpdf_extract_pages or nexpdf_open_office) give you a raw pointer that the Dart NexPdfDocument cannot wrap. Use it with further C calls and free it with nexpdf_free.

On this page