Let's learn how to open a PDF file from assets folder in android studio using Kotlin language. If you want to open PDF files in your android applications you can follow this guide.
We are using one dependency to get this task done. Just add the dependency and create or update xml layout file then finally let's open the PDF file grammatically in our android Kotlin applications.
Task Break up
Add Dependency
Add the below mentioned dependency line in your project module level build.gradle file, then Sync the gradle.
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
Modify or Create Activity Layout.
Modify the default actvity_main.xml or you can also create new one upto your project demands.
Use the PDFView as below
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:screenOrientation="portrait"
android:background="@color/bgColor"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="match_parent"/>
</RelativeLayout>
Kotlin Code
It's time to code for using PDF file in our android project. Here we are coding to open a PDF file from asset folder, so make sure your PDF file is ready in your project asset directory, if not create one.
val assetManager = this.assets
val inputStream = assetManager.open("Introduction.pdf")
pdfView.fromStream(inputStream).load()