PdfDocument class

A loaded PDF document.

PdfDocument is the top-level Dart abstraction for a PDF file. It mirrors the PDFium model: FPDF_LoadMemDocument64 returns a single document handle used for all subsequent operations. PdfDocument is the Dart owner of that handle and exposes document-level capabilities as async methods.

Loading

Use fromBytes to load a document from raw PDF bytes:

final bytes = await File('document.pdf').readAsBytes();
final doc = await PdfDocument.fromBytes(bytes);

Error handling

fromBytes throws PdfExtractionException when the document cannot be loaded. Inspect PdfExtractionException.error to distinguish between PdfError.passwordRequired and PdfError.invalidDocument so callers can give users an actionable message.

Resource management

Always call close when the document is no longer needed to release the native PDFium handle:

final doc = await PdfDocument.fromBytes(bytes);
try {
  final meta = await doc.getMetadata();
  // use meta…
} finally {
  await doc.close();
}

A Finalizer is registered internally as a safety net in case close is forgotten, but explicit disposal is strongly preferred.

Platform support

The public API is identical on all platforms. The backend differs:

Platform Backend
iOS, Android, macOS, dart:ffi + PdfiumIsolate
Linux, Windows
Web PDFium WASM

On native platforms all PDFium calls run on a dedicated Isolate so the caller's isolate (typically the UI isolate) is never blocked. On web, dart:isolate is not supported on any compile target, so PDFium calls run inside a dedicated Web Worker instead, communicating with the main thread via a hand-rolled postMessage protocol.

Future capabilities

PdfDocument is designed to be the foundation for future capabilities: text extraction (document.openTextExtractor()), annotation access, and page rendering. This plan establishes the class and its metadata surface; future plans add capabilities without breaking the existing API.

Properties

hashCode int
The hash code for this object.
no setterinherited
pageCount Future<int>
The total number of pages in the document.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
tableOfContents Future<List<PdfTocEntry>>
Returns the complete Table of Contents (bookmark/outline tree) for the document.
no setter

Methods

close() Future<void>
Closes the document and releases the native PDFium handle.
extractAnnotations({int? pageIndex}) Stream<PdfPageAnnotations>
Extracts all annotations from one or all pages of the document.
extractImages({int? pageIndex, bool includeBitmap = false}) Stream<PdfPageImages>
Extracts all image objects from one or all pages of the document.
extractPlainText({int? pageIndex, PdfTextExtractorConfig config = const PdfTextExtractorConfig()}) Stream<PdfPageText>
Extracts plain text from one or all pages of the document.
getDocumentInfo() Future<PdfDocumentInfo>
Returns document-level properties: PDF file version and file identifiers.
getMetadata() Future<PdfMetadata>
Returns the metadata extracted from the PDF Info dictionary.
getPageSize(int pageIndex) Future<PdfPageSize>
Returns the intrinsic size of a page in PDF user units (points).
getThumbnail(int pageIndex, {bool generateIfAbsent = true, int maxDimension = 256}) Future<PdfThumbnail?>
Returns a thumbnail image for the page at pageIndex.
isPlainTextExtractable({PdfTextExtractorConfig config = const PdfTextExtractorConfig()}) Future<bool>
Returns true when fewer than PdfTextExtractorConfig.scannedPageRatio of pages lack a text layer (i.e. the document is suitable for plain-text extraction).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
renderImage(int pageIndex, int objectIndex) Future<PdfImageBitmap?>
Fetches the rendered BGRA bitmap for a single image object on a page.
renderPageToBytes(int pageIndex, int pixelWidth, int pixelHeight, {bool renderAnnotations = true, bool lcdText = false, int backgroundColor = 0xFFFFFFFF}) Future<({int pixelHeight, int pixelWidth, Uint8List pixels})>
Renders a page to a raw BGRA pixel buffer.
Searches the document for query and streams all matches.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

fromBytes(Uint8List bytes, {String? dylibPath}) Future<PdfDocument>
Loads a PDF document from raw bytes.