Interacting with DocumentView

Nutrient provides two ways to interact with DocumentView in your Android Jetpack Compose application:

  1. DocumentManager

  2. DocumentConnection

Document manager

DocumentManager provides multiple lifecycle and annotation callbacks. To access DocumentManager, developers can make use of the getDefaultDocumentManager() method provided by default by the SDK.

getDefaultDocumentManager() provides documentListener, annotationListener, and uiListener. The following code snippet shows how to access them:

val documentState = rememberDocumentState(uri, configuration)

DocumentView(
    documentState = documentState,
    documentManager = getDefaultDocumentManager(
        documentListener = DefaultListeners.documentListeners(onDocumentLoaded = {
            ...
        }),
        annotationListener = DefaultListeners.annotationListeners(onAnnotationSelected = { annotation, _ ->
            ...
        }),
        uiListener = DefaultListeners.uiListeners(onImmersiveModeEnabled = { enabled ->
            ...
        })
    )
)

The code below shows some of the document and annotation callbacks that are supported with DocumentManager. We regularly expand on these, so it’s best to refer to the API documentation for the most up-to-date features:

@Immutable
class DocumentListener(
    val onDocumentLoaded: ((PdfDocument) -> Unit)? = null,
    val onDocumentLoadFailed: ((Throwable) -> Unit)? = null,
    val onDocumentSave: ((PdfDocument, DocumentSaveOptions) -> Boolean)? = null,
    val onDocumentSaved: ((PdfDocument) -> Unit)? = null,
    val onDocumentSaveFailed: ((PdfDocument, Throwable) -> Unit)? = null,
    val onDocumentSaveCancelled: ((PdfDocument?) -> Unit)? = null,
    val onPageClick: ((PdfDocument, Int, MotionEvent?, PointF?, Annotation?) -> Boolean)? = null,
    val onDocumentClick: (() -> Boolean)? = null,
    val onPageChanged: ((PdfDocument, Int) -> Unit)? = null,
    val onDocumentZoomed: ((PdfDocument, Int, Float) -> Unit)? = null,
    val onPageUpdated: ((PdfDocument, Int) -> Unit)? = null
)

@Immutable
class AnnotationListener(
    val onPrepareAnnotationSelection: ((AnnotationSelectionController, Annotation, Boolean) -> Boolean)? = null,
    val onAnnotationSelected: ((Annotation, Boolean) -> Unit)? = null,
    val onAnnotationDeselected: ((Annotation, Boolean) -> Unit)? = null
)

@Immutable
class UiListener(
    val onImmersiveModeEnabled: ((Boolean) -> Unit)? = null,
    val onDocumentScroll: ((ScrollState) -> Unit)? = null
)

Document connection

DocumentConnection provides various actions that can be provided by DocumentView. It can be accessed via documentState, like so:

val documentState = rememberDocumentState(uri, pdfActivityConfiguration)
Column {
    DocumentView(documentState = documentState)
    Button(onClick = { documentState.documentConnection.setPageIndex(2) }) {
       Text("Go to page number 2")
    }
}

Currently DocumentConnection provides two actions: setPageIndex() and addAnnotationToPage().