Getting started
Activate a license, open a PDF, render a page, search it and show it in a viewer with NexPDF for Flutter.
This example opens a PDF from the app's assets, shows its first page, prints the search hits for a word and opens the pure-Flutter viewer. It runs without a license key too: viewing is always allowed, and rendered pages then carry a watermark.
Add the package
Add nexpdf_flutter and set up iOS and Android as described in
Installation. Add assets/sample.pdf (and, if you have one,
assets/nexpdf_license.xml) to the flutter.assets list in pubspec.yaml.
Write the app
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';
final sdk = NexPdfFlutter();
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// 1. Activate the license once per launch (optional for viewing).
try {
final key = await rootBundle.loadString('assets/nexpdf_license.xml');
sdk.activateLicense(key, appId: 'com.acme.app');
} catch (e) {
debugPrint('NexPDF license: $e');
}
// 2. Open the document.
final bytes = (await rootBundle.load('assets/sample.pdf')).buffer.asUint8List();
final doc = sdk.open(bytes);
// 3. Page count, size and search.
final size = doc.pageSize(0);
debugPrint('${doc.pageCount} pages, first page ${size.width} x ${size.height} pt');
for (final hit in doc.search('total')) {
debugPrint('page ${hit.page + 1}: ${hit.context}');
}
runApp(MaterialApp(home: FirstPage(doc: doc)));
}
class FirstPage extends StatelessWidget {
const FirstPage({super.key, required this.doc});
final NexPdfDocument doc;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('sample.pdf')),
// 4. Render the first page at 144 dpi.
body: Center(child: Image.memory(doc.renderPagePng(0, dpi: 144))),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.menu_book),
// 5. Open the pure-Flutter viewer.
onPressed: () => Navigator.push(context,
MaterialPageRoute(builder: (_) => NexPdfViewerWidget(document: doc))),
),
);
}
}Run it
flutter runWhat the code does
NexPdfFlutter()loads the engine. Create one and keep it for the life of the app.sdk.activateLicense(license, appId:)accepts the license XML or the bare key. The app id is the bundle id on iOS and the package name on Android. See License.sdk.open(bytes, password:)opens a PDF; JPEG, PNG and TIFF pictures become one page each.sdk.openFile(path)reads a file first.pageSize(i)is the displayed size in points as a(width, height)record: crop box, rotation and/UserUnitapplied.renderPagePng(i, dpi:)returns PNG bytes. 72 dpi is one pixel per point; the default is 150.search(query)returnsNexPdfSearchHits with the page, the context and the rectangle.NexPdfViewerWidgetshows the document with page swiping, pinch zoom, search with highlights and a pages grid. It does not dispose the document.
To show the full native reader instead, see Reader.