License
Activate a NexPDF license key in a Flutter app, read the license status and handle license errors.
Activate the license once per launch, before other NexPDF calls. The key is accepted as the
license XML file's text or the bare NEX2. key. Keys are bound to a platform (ios or android,
and macos, windows or linux on desktop) and to the app id: the bundle id on iOS, the package
name on Android. See Licensing for how keys are issued and bound.
Activate
import 'package:flutter/services.dart';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';
final sdk = NexPdfFlutter();
final key = await rootBundle.loadString('assets/nexpdf_license.xml');
sdk.activateLicense(key, appId: 'com.acme.app');| Parameter | Value |
|---|---|
license | The license XML or the bare key |
appId | The bundle id (iOS) or package name (Android) the key was issued for |
platform | Defaults to NexPdfFlutter.platform: ios, android, macos, windows or linux |
activateLicense returns 2 for a commercial key and 1 for a trial key. A rejected key throws
NexPdfException with the engine's reason:
try {
final status = sdk.activateLicense(key, appId: 'com.acme.app');
debugPrint(status == 1 ? 'trial' : 'licensed');
} on NexPdfException catch (e) {
debugPrint('NexPDF license rejected: $e');
}The license is process-wide, so it also covers the native reader on iOS and Android.
Fixed in 1.0.1
In 1.0.0, activateLicense threw for every result other than 1, so a valid commercial key looked
like a failure. Since 1.0.1 it accepts any positive result and returns it. See the
changelog.
The exception carries the message only. The codes behind the messages are:
| Code | Meaning |
|---|---|
| 2 | Commercial key, active |
| 1 | Trial key, active (pages keep the watermark) |
| -1 | Malformed key |
| -2 | Bad signature |
| -3 | Unsupported key version |
| -4 | Expired |
| -5 | Not yet valid (check the clock) |
| -6 | Wrong platform |
| -7 | Wrong application id |
Check the status
The Dart API has no status method. Read nexpdf_license_status_json through dart:ffi:
import 'dart:convert';
Map<String, Object?> licenseStatus() {
final status = nexpdfLib.lookupFunction<Pointer<Utf8> Function(), Pointer<Utf8> Function()>(
'nexpdf_license_status_json');
return jsonDecode(takeString(status()) ?? '{}') as Map<String, Object?>;
}
final s = licenseStatus();
print('${s['state']} ${s['tier']} ${s['days_left']}');The status has state (unlicensed, trial, licensed), licensee, id, tier, modules,
expires_at, days_left and watermark.
Missing modules
A call that needs a module your key does not include fails, and sdk.lastError() says why, for
example the active license does not include the 'forms' module. Without a license the SDK stays
in viewer mode: it opens, renders and searches, and rendered pages carry the
"NexPDF • Unlicensed" watermark.