Référence de l'API
Chaque classe et méthode du plugin Flutter NexPDF, et comment appeler depuis Dart, avec dart:ffi, n'importe quelle fonction de l'API C de NexPDF.
Tout ce qui suit est exporté par package:nexpdf_flutter/nexpdf_flutter.dart. Le plugin est en
version 1.0.0.
NexPdfFlutter
Charge le moteur, active la licence et ouvre les 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})| Membre | |
|---|---|
NexPdfFlutter([library]) | Charge le moteur : le processus sur iOS, libnexpdf_ffi.dylib sur macOS, nexpdf_ffi.dll sur Windows, libnexpdf_ffi.so sur Android et Linux, ou la DynamicLibrary que vous passez |
platform | La plateforme de licence de cet appareil : ios, android, macos, windows ou linux |
activateLicense | Active une clé ou un XML de licence pour appId. Lève NexPdfException si le résultat n'est pas 1 ; voir la note dans Licence |
lastError() | Le message du dernier appel en échec sur ce thread, ou null |
open | Ouvre des octets PDF, ou des images JPEG, PNG et TIFF (une page chacune). Lève NexPdfException, par exemple password required ou incorrect password |
openFile | Lit un fichier et l'ouvre |
Les méthodes à handle brut pageCount(Pointer<Void>), renderPagePng(Pointer<Void>, int, {dpi}) et
disposeDocument(Pointer<Void>) sont conservées pour le code écrit pour la version 0.1.0.
NexPdfDocument
Un document ouvert. Non thread-safe : utilisez-le depuis un seul 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()| Membre | Module | |
|---|---|---|
handle | Le NexPdfDocument* natif, pour les appels C. Lève StateError après dispose() | |
pageCount | viewer | Nombre de pages |
pageSize | viewer | Taille affichée en points : zone de recadrage, /Rotate et /UserUnit appliqués |
pageView | viewer | La géométrie d'affichage et les transformations d'une page |
renderPagePng | viewer | Octets PNG à dpi. Lève NexPdfException en cas d'échec |
save | viewer | Le document avec toutes les modifications, sous forme d'octets PDF |
toMarkdown | viewer | Le texte en Markdown : titres, listes et tableaux |
search | viewer | Recherche insensible à la casse, dans l'ordre de lecture |
ask | viewer | Recherche hors ligne : les k meilleurs passages et un context balisé par page pour un modèle de langage |
dispose | Libère le document. À défaut, un finaliseur le libère |
NexPdfPageView
Comment une page est affichée, et la transformation entre l'espace utilisateur PDF et les coordonnées d'affichage (origine en haut à gauche de la page telle qu'affichée, y vers le bas).
| Membre | |
|---|---|
width, height | Taille affichée en points |
rotation, userUnit | /Rotate et /UserUnit |
crop | La zone de recadrage (llx, lly, urx, ury) dans l'espace utilisateur PDF |
toDisplay | La matrice [a, b, c, d, e, f] de l'espace utilisateur PDF vers les points d'affichage |
toView(x, y, [scale]) | Un point PDF vers des pixels d'affichage à scale pixels par point |
toPdf(vx, vy, [scale]) | Des pixels d'affichage vers un point PDF, par exemple un appui |
deltaToPdf(dx, dy, [scale]) | Un déplacement à l'écran vers un déplacement dans l'espace PDF |
rectToView(llx, lly, urx, ury, [scale]) | Un rectangle PDF vers des pixels (left, top, right, bottom) |
rectToPdf(x1, y1, x2, y2, [scale]) | Deux coins d'affichage vers un (llx, lly, urx, ury) PDF |
NexPdfPageView.plain(width, height) | Une page sans rotation, d'origine 0,0 |
NexPdfPageView.fromJson(json) | À partir du JSON de nexpdf_page_view_json |
NexPdfSearchHit
page (à partir de 0), context (le texte autour de l'occurrence) et llx, lly, urx, ury (le
rectangle en points PDF).
NexPdfException
Levée quand un appel NexPDF échoue. message est l'erreur du moteur.
NexPdfReader
Le lecteur natif des SDK iOS et Android. Voir Lecteur.
static Stream<NexPdfReaderEvent> get events
static Future<void> open({Uint8List? document, String fileName = 'document.pdf', NexPdfUIConfig config = const NexPdfUIConfig()})
static Future<void> close()NexPdfReaderEvent a type et 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);Une visionneuse en pur Flutter. Elle ne libère pas document.
NexPdfUIConfig
La configuration d'interface commune.
| Membre | |
|---|---|
NexPdfUIConfig([json]), NexPdfUIConfig.fromJson(map) | À partir d'une map JSON |
NexPdfUIConfig.parse(source) | À partir de texte JSON ; un JSON invalide donne les valeurs par défaut |
json | La map brute, transmise telle quelle aux lecteurs natifs |
toJsonString() | La configuration sous forme de texte JSON |
readOnly, initialMode, availableModes, modeAllowed(mode) | Réglages des modes |
hiddenItems, hidden(id), customItems, customItemsAt(location, {mode}) | Réglages de la barre d'outils |
annotationTools, formTools, annotationToolAllowed(id), formToolAllowed(id) | Listes d'outils |
defaultColor, colorPalette, stamps | Valeurs par défaut des annotations |
themeMode, accentColor, language, string(key, language) | Thème et langue |
NexPdfUIConfig.parseColor(hex) | #RRGGBB ou #RRGGBBAA vers une Color |
NexPdfCustomItem est un bouton personnalisé : id, title, icon, location (top, toolbar
ou more, la valeur par défaut) et modes.
Appeler l'API C
Toute autre fonction du moteur est une fonction C de nexpdf.h, fourni dans
NexPdfCore.xcframework et dans le dossier include/ des archives nexpdf-ffi. Appelez-la avec
dart:ffi sur doc.handle. Placez une fois ces utilitaires dans votre application :
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;
}Recherchez ensuite une fonction avec sa signature C et appelez-la :
// 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))));| Type C | Type natif | Type Dart |
|---|---|---|
NexPdfDocument * | Pointer<Void> | Pointer<Void> |
const char * | Pointer<Utf8> | Pointer<Utf8> |
char * (renvoyé) | Pointer<Utf8> | Pointer<Utf8>, puis takeString |
NexPdfBytes (renvoyé) | NexPdfBytes | NexPdfBytes, puis takeBytes |
const uint8_t *, size_t len | Pointer<Uint8>, Size | Pointer<Uint8>, int |
size_t | Size | int |
int32_t, uint32_t | Int32, Uint32 | int |
double | Double | double |
Les fonctions qui renvoient un nouveau NexPdfDocument * (comme nexpdf_extract_pages ou
nexpdf_open_office) vous donnent un pointeur brut que le NexPdfDocument Dart ne peut pas
encapsuler. Utilisez-le avec d'autres appels C et libérez-le avec nexpdf_free.