This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/flutter/open-a-document.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Flutter open PDF file | Nutrient SDK

Nutrient Flutter SDK enables you to open documents with the NutrientDocumentView widget. You can also open documents without a user interface (UI) for headless processing with Nutrient.openDocument().

Opening a PDF with a viewer

To open a PDF file stored as an asset, copy it to a temporary directory first.

The following helper function copies the file to a temporary directory with the path_provider(opens in a new tab) package:

import 'dart:io';
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:path_provider/path_provider.dart' as path_provider;
Future<String> extractAsset(String assetPath) async {
// On Web, we can directly use the asset path.
if (kIsWeb) return assetPath;
final bytes = await rootBundle.load(assetPath);
final list = bytes.buffer.asUint8List();
final tempDir = await path_provider.getTemporaryDirectory();
final tempDocumentPath = '${tempDir.path}/$assetPath';
final file = await File(tempDocumentPath).create(recursive: true);
file.writeAsBytesSync(list);
return file.path;
}

After you copy the asset, pass the document path to NutrientDocumentView:

final documentPath = await extractAsset(assetPath);
Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(),
body: NutrientDocumentView(documentPath: documentPath),
);

For more information about opening documents with Nutrient Flutter SDK, refer to the getting started guide. To explore sample implementations, refer to the example projects guide.

The global Nutrient.present() method is part of the legacy method-channel API and isn’t available on the SDK 6 bindings. Push a route that contains a NutrientDocumentView instead. For migration information, refer to the upgrade guide.

Opening an image

Nutrient Flutter SDK can open and annotate PNG and JPEG images. Pass the image path to NutrientDocumentView as you would for a PDF. Nutrient detects the format from the file extension:

final imagePath = await extractAsset(assetImagePath);
NutrientDocumentView(documentPath: imagePath);

Document loaded callback

Use the onControllerReady callback to access the controller. Then listen to the documentLoaded(opens in a new tab) event to get notified when Nutrient loads the document:

NutrientDocumentView(
documentPath: documentPath,
onControllerReady: (controller) {
controller.events.documentLoaded.listen((event) async {
final pageCount = await event.document.getPageCount();
print('Document loaded with $pageCount pages');
});
},
)

Headless processing

Use Nutrient.openDocument() to open and manipulate PDF documents without displaying a viewer UI:

import 'package:nutrient_flutter/bindings.dart';
void processDocument(String documentPath) async {
final document = await Nutrient.openDocument(documentPath);
try {
final pageCount = await document.getPageCount();
print('Document has $pageCount pages');
// Perform operations...
} finally {
await document.close();
}
}

For password-protected documents, pass the password when you open the document:

final document = await Nutrient.openDocument(
documentPath,
password: 'secret123',
);

Available operations

The returned document exposes the same API as the viewer’s controller.document. Refer to the NutrientDocumentInterface(opens in a new tab) API reference for the complete interface.

Common operations include:

  • Annotations — Use document.annotations for annotation CRUD, search, and XFDF. Refer to the annotations guide.
  • Form fields — Use document.forms for form filling and reading. Refer to the forms guide.
  • Bookmarks — Use document.bookmarks for bookmark CRUD. Refer to the bookmarks guide.
  • Document-level operations — Use save(), exportPdf(), exportInstantJson(), applyInstantJson(), getPageCount(), and getPageInfo().

To flatten, embed, or remove annotations without opening a document, use the static Nutrient.processAnnotations() method. It reads from a source path and writes the processed result to a destination path.

Saving and closing

Headless documents require explicit saving and closing:

final hasChanges = await document.hasUnsavedChanges();
if (hasChanges) {
await document.save();
}
await document.close();

Headless vs. viewer

Use this table to compare headless processing with the viewer:

FeatureHeadlessViewer
UI displayedNoYes
Auto-saveNoYes
Resource cleanupManual (close())Automatic