Kotlin下载并显示PDF

问题描述 投票:0回答:1

[嗨,我想使用Kotlin下载并显示pdf。我想在下载时下载并在pdf显示的第一页上显示进度条,然后显示PDF。我使用PDFKit迅速完成了它,这非常简单,但是我在Kotlin中找不到等效项。

我进行了很多研究以在Kotlin中显示pdf,但没有得到太多结果,似乎这个主题并不是真的,所以我首先看了本机的PdfRenderer,但大多数示例使用的是Java而不是Kotlin,而我没有没有得到什么:documented.getSeekableFileDescriptor()。

然后,我查看了pdfView库,它非常适合显示pdf,但仅从资产显示,因此pdfView.fromStream似乎无法正常工作,我无法举例说明它的工作方式。我想进一步了解避免下载pdf文件,我想直接显示它以避免长时间加载。

最后,我使用okhttp进行了改造并下载了pdf,但是我不能使用pdfView来显示它,因为在from资产中pdf必须已经在项目中。

[我发现从和从网址下载pdf并用Kotlin进行显示非常困难,而且文档也很少。

因此,如果有人有任何建议,我会接受。

这是我使用pdfView.fromStream的代码示例,仅加载空白页面

 private fun loadpdf(){
        println("pdfview")
        //PDF View
        Thread(Runnable {
            val input = URL(pdf_url).openStream()
            val pdfView = this.findViewById<PDFView>(com.example.mylibrary.R.id.pdfView)
            //pdfView.fromFile(file)
            pdfView.fromStream(input)
                .enableSwipe(true) // allows to block changing pages using swipe
                .swipeHorizontal(true)
                .enableDoubletap(true)
                .defaultPage(0)
                .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                .password(null)
                .scrollHandle(null)
                .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                // spacing between pages in dp. To define spacing color, set view background
                .spacing(0)
                .pageFitPolicy(FitPolicy.WIDTH)
                .load()
            println("testpdf")
        })
    }

这是我使用pdfView.fromAsset的代码示例,这项工作有效,但前提是文件已经在项目中,但是我想从url中获取我的pdf

private fun loadpdf(){
        //PDF View
        Thread(Runnable {
            val pdfView = this.findViewById<PDFView>(com.example.mylibrary.R.id.pdfView)
            pdfView.fromAsset("url")
                .enableSwipe(true) // allows to block changing pages using swipe
                .swipeHorizontal(true)
                .enableDoubletap(true)
                .defaultPage(0)
                .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                .password(null)
                .scrollHandle(null)
                .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                // spacing between pages in dp. To define spacing color, set view background
                .spacing(0)
                .pageFitPolicy(FitPolicy.WIDTH)
                .load()
        })
    }
kotlin retrofit2 okhttp pdfrenderer
1个回答
0
投票

[如果有人遇到这个问题,我可以使用协程解决它:将此依赖项添加到您的Gradle构建中:实现(“ org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2”)

然后导入:在文件中导入kotlinx.coroutines。*。

然后使用劳氏协同程序方法如下:

private fun loadpdf(pdfView: PDFView){
        //PDF View
        GlobalScope.launch{
            val input : InputStream
            input = URL(pdf_url).openStream()
            pdfView.fromStream(input)
                .enableSwipe(true) // allows to block changing pages using swipe
                .swipeHorizontal(true)
                .enableDoubletap(true)
                .defaultPage(0)
                .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                .password(null)
                .scrollHandle(null)
                .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                // spacing between pages in dp. To define spacing color, set view background
                .spacing(0)
                .pageFitPolicy(FitPolicy.WIDTH)
                .load()
        }

您必须传递pdfView cos,才能在asyctask中使用全局视图。

现在添加进度条,您可以将其添加到xml中:

<ProgressBar
            android:id="@+id/Progressbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:indeterminate="true"
            android:minWidth="200dp"
            android:minHeight="50dp" />

然后,您需要重写loadComplete函数以获取进度条的回调,并为您的活动实现OnLoadCompleteListener接口。

和顺便说一句,如果您想使用pdfView lib添加每页显示页面,请添加:.swipeHorizo​​ntal(true).pageSnap(true).autoSpacing(true).pageFling(true)

© www.soinside.com 2019 - 2024. All rights reserved.