github源码文件
添加依赖

 implementation "com.github.barteksc:android-pdf-viewer:3.2.0-beta.1"

在compose中展示pdf文件

   AndroidView(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(600.dp),
                factory = { ctx ->
                    val parser: XmlPullParser = ctx.resources.getXml(R.xml.file_path)
                    val attributes = Xml.asAttributeSet(parser)
                    PDFView(ctx, attributes).apply {
                        isLoading = true
                    }

                },
                update = { pdfview ->
                    pdfview//.fromUri(Uri.parse(url))
                        .fromFile(File(pathAndroidPdf))
                        .defaultPage(0)
                        .onPageChange { page, pageCount ->

                        }
                        .enableAnnotationRendering(false)
                        .onLoad {
                            Log.d(TAG, "PreviewUriPage: $it")
//                        onLoadComplete(it)
                            isLoading = false
                        }
                        .spacing(10)
                        .pageFitPolicy(FitPolicy.BOTH)
                        .load()

                })

线上文件使用fromUri(Uri.parse(url))
本地文件使用.fromFile(File(pathAndroidPdf))
问题:
对于pdf文件过大的情况,渲染时间过长,在渲染过程中,没有找到对应的方法

在xml View中展示pdf文件
view:

  <com.github.barteksc.pdfviewer.PDFView
                android:id="@+id/fl_pdf"
                android:layout_width="match_parent"
                android:layout_height="600dp"
                android:visibility="visible"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                />

实现

binding.flPdf.fromUri(Uri.parse(it))
               // .fromFile(File(pathAndroidPdf))
                .defaultPage(0)
                .onPageChange { page, pageCount ->

                }
                .enableAnnotationRendering(false)
                .onLoad {
                showLoading(true)
                }
                .onRender {
                    showLoading(false)
                }
                .spacing(10)
                .pageFitPolicy(FitPolicy.BOTH)
                .load()
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐