1 year ago

#301667

test-img

ninhnau19

Best way to preview page of Pdf file in Android

I'm developing a feature to preview the pages of a Pdf file in Android. I tried with PdfRenderer using the recyclerview to show the list page, and the problem is it causes lag in the app.

My question is: What's the best way to preview the pages of a Pdf file? , or can my code improve anything to avoid lag?

Here's my code:

Create a renderer in the activity

val fileDescriptor: ParcelFileDescriptor?
fileDescriptor = ParcelFileDescriptor.open(
    File(path),
    ParcelFileDescriptor.MODE_READ_ONLY
)
if (fileDescriptor != null) {
    renderer = PdfRenderer(fileDescriptor)
}
     

Pass renderer into a DiaLog

val splitPageSelectDialog = SplitPageSelectDialog(this, numberPage, renderer) {
}

Pass renderer into an Adapter

mAdapter = new PageAdapter(getContext(), mOptionList, renderer, this);
}

And bind data in ViewHolder

val page = renderer.openPage(position)
val width = page.width
val height = page.height
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
page.close()
binding.imgPreviewPage.setImageBitmap(bitmap)

Finally, I got something like this: picture

Can anyone help me here?

android

android-recyclerview

android-dialog

pdfrenderer

0 Answers

Your Answer

Accepted video resources