Interacting with DocumentView

PSPDFKit 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 and annotationListener. 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, _ ->
            ...
        })
    )
)

The code below shows various document and annotation callbacks, which are supported with DocumentManager:

@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
)

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().