Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use PSPDFKit to open a PDF file in Jetpack Compose, you can follow these steps:

  1. Add the PSPDFKit dependency to your project. You can do this by adding the following line to your app/build.gradle file:
implementation "com.pspdfkit:pspdfkit:7.6.0"
  1. Create a PSPDFKit configuration object, which includes your license key. For example:
val config = PSPDFKit.Configuration.Builder(context)
        .licenseKey("YOUR_LICENSE_KEY")
        .build()
  1. Create a PSPDFKitFragment instance, passing in the configuration and the path to your PDF file. For example:
val fragment = PSPDFKitFragment.newInstance(config, "/sdcard/my-pdf-file.pdf")
  1. Create a Composable function that uses the AndroidView component to display the PSPDFKitFragment. For example:
@Composable
fun PdfView() {
    AndroidView(
        factory = { context ->
            PSPDFKitFragment.newInstance(config, "/sdcard/my-pdf-file.pdf")
        }
    )
}
  1. Use the PdfView composable function in your Jetpack Compose UI hierarchy to display the PDF file. For example:
@Composable
fun MyScreen() {
    Column {
        Text("PDF Viewer")
        PdfView()
    }
}

And that's it! You should now be able to open and display a PDF file using PSPDFKit in Jetpack Compose.