Nutrient Flutter SDK enables you to open documents using NutrientView, the Nutrient.present() method, or headless processing with Nutrient.openDocument().

Opening a PDF with a viewer

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

The following helper function copies the file to a temporary directory:

Future<File> extractAsset(String assetPath) async {
// On Web, we can directly use the asset path.
if (kIsWeb) return File(assetPath);
final bytes = await DefaultAssetBundle.of(context).load(assetPath);
final list = bytes.buffer.asUint8List();
final tempDir = await Nutrient.getTemporaryDirectory();
final tempDocumentPath = '${tempDir.path}/$assetPath';
final file = await File(tempDocumentPath).create(recursive: true);
file.writeAsBytesSync(list);
return file;
}

To open a PDF document using NutrientView:

final extractedDocument = await extractAsset(_documentPath);
Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(),
body: NutrientView(documentPath: extractedDocument.path),
);

To open a PDF document using the global present() method:

final document = await extractAsset(documentPath);
await Nutrient.present(document.path);

The present() method is a global method that works from anywhere in the app. It’s only supported on iOS and Android.

For more details about how to open a document using Nutrient Flutter SDK, see our getting started guides or try our Catalog example project.

Opening an image

In addition to PDF documents, Nutrient Flutter SDK also enables you to open and annotate images (PNG and JPEG). The following example shows how to open a PNG image using the present() method:

final extractedImage = await extractAsset(imagePath);
await Nutrient.present(extractedImage.path);

Document loaded callback

Use the onDocumentLoaded callback to get notified when a document has been loaded. This is called with the PdfDocument(opens in a new tab) object as an argument:

NutrientView(
documentPath: extractedDocument.path,
onDocumentLoaded: (PdfDocument pdfDocument) {
print('Document loaded: ${pdfDocument.documentId}');
},
);

Headless processing

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

import 'package:nutrient_flutter/nutrient_flutter.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:

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

Available operations

With a PdfDocument instance, the following operations are available:

  • AnnotationsgetAnnotations(), addAnnotation(), removeAnnotation(), processAnnotations()
  • Form fieldsgetFormFields(), getFormFieldValue(), setFormFieldValue()
  • BookmarksgetBookmarks(), addBookmark(), removeBookmark()
  • Export/ImportexportInstantJson(), applyInstantJson(), exportXfdf(), importXfdf(), exportPdf()

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

The following table compares headless processing with the viewer:

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