Skip to main content
KPdfViewerState is the main interface you interact with after creating a viewer. It exposes the document’s loading and rendering state through StateFlow properties, and provides methods to navigate pages, control zoom, open files from the device, save the document, hand it off to an external app, render individual pages off-screen, and release resources. One KPdfViewerState instance maps to one visible viewer; share the same instance across KPdfViewer, KPdfThumbnailStrip, and KPdfViewerToolbar so they stay in sync. In Compose, obtain an instance through rememberPdfViewerState. For non-Compose contexts, use KPdfFactory.

Observable properties

These properties expose the current state of the viewer. Collect them with collectAsState() in Compose or with collect {} in a coroutine scope outside Compose.
KPdfSource
required
The source that is currently loaded or being loaded. Updated synchronously when you call open(source).
KPdfViewerConfig
required
The viewer configuration that was provided when this state was created. Immutable for the lifetime of the state instance.
StateFlow<KPdfLoadState>
required
Tracks the overall document loading lifecycle. Observe this to show loading indicators and handle errors.
StateFlow<Int>
required
The 0-based index of the page currently displayed in the viewer.
StateFlow<Float>
required
The active zoom level as a Float. The range is determined by the minZoom and maxZoom values in KPdfViewerConfig.
StateFlow<KPdfRenderedPageState>
required
The rendering state of the currently active page surface. Use this to react to render completion or render failures.
StateFlow<KPdfOpenDocumentState>
required
Tracks the state of a device-picker open request initiated by requestOpenFromDevice(). Progresses through Idle, AwaitingSelection, Success, Cancelled, and Error. See KPdfOpenDocumentState.
StateFlow<KPdfSaveState>
required
Tracks the state of a save request initiated by requestSave() or savePdf(). Progresses through Idle, Exporting, AwaitingDestination, Success, Cancelled, and Error. See KPdfSaveState.
StateFlow<KPdfExternalOpenState>
required
Tracks the state of an external-app open request initiated by requestOpenInExternalApp() or openInExternalApp(). Progresses through Idle, Exporting, AwaitingExternalApp, Success, Cancelled, and Error. See KPdfExternalOpenState.

Methods

open

Opens a PDF source, cancelling any in-flight load or render work owned by this state instance.
KPdfSource
default:"this.source"
The source to open. Defaults to the source already held by this state, which means calling open() with no arguments reloads the current document.

retry

Retries the most recent open request. Call this when loadState reports a failure and you want to give the user a way to try again without changing the source.

nextPage

Advances the viewer to the next page. Has no effect when the last page is already displayed.

previousPage

Moves the viewer back to the immediately preceding page. Has no effect when the first page is already displayed.

goToPage

Navigates directly to a specific page by its 0-based index.
Int
required
The 0-based index of the target page. Page 1 of the document is index 0.

setZoom

Sets the zoom level for the active page surface to an exact value.
Float
required
The target zoom level. Should be within the minZoommaxZoom range defined in KPdfViewerConfig; values outside the range are coerced.

zoomIn

Increases the current zoom by one viewer-defined step.

zoomOut

Decreases the current zoom by one viewer-defined step.

resetZoom

Resets the zoom back to the configured minimum zoom level.

requestOpenFromDevice

Triggers the platform’s native document picker so the user can select a PDF from local storage. When the user completes the picker flow, openDocumentState transitions to Success with the selected source. Call open(selectedSource) to load it.
List<String>
default:"listOf(\"application/pdf\")"
The MIME types accepted by the platform picker. Defaults to PDF only.

requestSave

Requests that the SDK export the current PDF and trigger the platform’s save/destination picker. Observe saveState to react to the outcome.
String?
default:"null"
The file name pre-filled in the platform save dialog. Pass null to let the platform decide.
String
default:"KPdfSaveRequest.DefaultMimeType"
The MIME type used when writing the file. Defaults to application/pdf.

savePdf

Alias for requestSave. Accepts the same parameters and delegates directly to it. Prefer whichever name reads more clearly at your call site.
String?
default:"null"
The file name pre-filled in the platform save dialog.
String
default:"KPdfSaveRequest.DefaultMimeType"
The MIME type used when writing the file.

requestOpenInExternalApp

Requests that the SDK export the current PDF and hand it to an installed app that can open PDF content (e.g., a system PDF viewer). Observe externalOpenState to react to the outcome.
String?
default:"null"
The file name suggested to the receiving app or OS share sheet.
String
default:"KPdfExternalOpenRequest.DefaultMimeType"
The MIME type used when handing off the file. Defaults to application/pdf.

openInExternalApp

Alias for requestOpenInExternalApp. Accepts the same parameters and delegates directly to it.
String?
default:"null"
The file name suggested to the receiving app or OS share sheet.
String
default:"KPdfExternalOpenRequest.DefaultMimeType"
The MIME type used when handing off the file.

renderPage

Renders a specific page to an off-screen bitmap. This is a suspend function; call it from a coroutine scope. Use it when you need pixel-level access to a page — for example, to feed a custom sharing flow — rather than to display it in the viewer.
Int
required
The 0-based index of the page to render.
Int
required
The desired width of the output bitmap in pixels.
Int
required
The desired height of the output bitmap in pixels.
Float
default:"1f"
The zoom factor to apply when rendering. Defaults to 1f (no zoom).
Returns Result<KPdfPageBitmap>. Check isSuccess before using the value.

exportPdf

Exports the currently configured PDF source as raw bytes. This is a suspend function. Use the returned bytes to implement a custom share flow that bypasses the SDK’s built-in save/external-open paths. Returns Result<ByteArray>.

close

Closes the active document and releases all resources owned by this state instance, including cached pages and coroutine work. Call this when the viewer is being torn down outside of Compose lifecycle management.