Skip to main content
KPdfLoadState is the observable load lifecycle for a single KPDF viewer instance. Collect it from KPdfViewerState.loadState (a StateFlow) to drive loading indicators, show page counts, surface error messages, and gate actions that require a fully decoded document. The state machine progresses linearly from IdleLoadingReady on success, or IdleLoadingError on failure. Transitioning the viewer to a new source resets the state back to Idle.

Variants

data object
No document is loaded and no load is in progress. This is the initial state of every viewer instance before open() is called, and the state the viewer returns to after close().
data object
The SDK is actively fetching (for URL sources) or decoding the PDF bytes. Long-running network requests remain in Loading for their entire duration. Display a progress indicator while in this state.
data class
The document has been fully decoded and the first page is ready to render.
Int
required
Total number of pages in the document. Always ≥ 1 when Ready is emitted.
String?
required
The document title extracted from PDF metadata, or null if no title is present in the file.
data class
Document loading failed. Inspect reason to determine the failure type and decide whether to surface a retry action or a specific error message.
KPdfError
required
A sealed KPdfError value describing the failure. See KPdfError variants below.

KPdfError variants

KPdfError is a sealed interface with a message: String property on every variant. The following concrete types are available:

Collecting load state in Compose

Call collectAsState() on KPdfViewerState.loadState inside your composable, then branch on each variant with a when expression.
pageCount is only available on the Ready variant. Guard any page-count logic with a smart cast or (loadState as? KPdfLoadState.Ready)?.pageCount to avoid a null or default value while the document is still loading.

Notes

  • KPdfViewerState.loadState is a StateFlow<KPdfLoadState>, so it always has a current value and never suspends on the first collection.
  • Calling viewerState.open(newSource) while in Ready or Error resets loadState to Idle before beginning the next load cycle.
  • If you only need pageCount for toolbar display, KPdfViewerToolbar reads it internally — you do not need to observe loadState yourself in that case.