A pdfviewer for android jetpack compose with search feature
- Single line of code
- Zooming
- Search word in page
- Share either file or page
- Smooth scrolling (horizontal/vertical)
- Remember last visited page
Below are steps to implement in your android studio compose project
Place below in repository{} block of settings.gradle.kts
maven {
url = uri("https://repo.itextsupport.com/android")
}
Place it in dependencies{} block of app level gradle file
implementation("io.github.farimarwat:pagepilot-pdfviewer:1.1")
There are two ways to use PagePilot Pdf Viewer.
- Already provided Templete which needs just single line of code.
- Customize by your self
val uri:Uri = ...
PdfView(fileuri = uri)
fun PdfView(
fileuri: Uri,
password: String? = null, //password if any
topbarcolor: Color = Color.White, //container color of top bar
iconscolor: Color = Color.Black, // icons color used in the view e.g. icons in top bar
counterbackgroundcolor: Color = Color.Gray, //page counter background color
OnError: (error: String) -> Unit = {}
)
To list pdf files, I have provided a utility to get all pdf files:
val types = arrayOf("application/pdf") // type of file to list. here I only want to list pdf files
val files = PagePilotUtils.listFiles(context,types)
This will return a list of PdfFile object data class which is already available in the package:
data class PdfFile(
val id:Long,
val path:String,
val uri:String,
val name:String,
val size:Long,
val datetime:Long,
val type:String
)
To list pdf files, you have to use:
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
Note:Sample project is also available in the repo. just clone it
To use PagePilot PdfViewer in customize way:
First get the main object and then use it methods:
val mPdfTool = PdfViewTool.Builder(context)
.build()
mPdfTool?.let { tool ->
if(tool.isProtected(uri)){ //this will check if file is protected
tool.openDocument(uri, password){ error ->
//log error here
}
} else {
tool.openDocument(uri){ error ->
//log error here
}
}
mPageCount = tool.getPageCount()
Log.e(TAG_PDFTOOL,"PageCount: $mPageCount")
}
It is necessary to close the document onstop of the activity
mPdfTool?.closeDocument()
mPdfTool?.getPageBitmap(
scaleFactor = scaleFactor, //this is to get scaled image. e.g. 1.0 for same size and 1.5 to upscale
index = index, //get the page e.g. to get the first page = 0
search = search // if you want to search a word the provide it else provide empty string = ""
)
mPdfTool?.getPageSize(index)
mPdfTool?.getPageCount()
mPdfTool?.getDetails()
This will return object of PdfDetails class
data class PdfDetails(
val author:String?,
val creator:String?,
val title:String?,
val keywords:String?,
val subject:String?,
val created:String?
)
I have no time to test it thoroughly. So do not panic if some issue occures. Kindly open an issue and I will try my best to solve it.