Android pdf viewer URL and Asset folder Kotlin example

07-Jan-2025

Android pdf viewer URL and Asset folder Kotlin example

Output:




1/  Add dependency on build.gradle file



implementation("com.github.DImuthuUpe:AndroidPdfViewer:3.1.0-beta.1")



2/  Add maven on settings.gradle file



maven(url = "https://jitpack.io")


3/  Enable jetifier on gradle.properties



android.enableJetifier=true


4/ Modify activity_main.xml file




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>


5/ Modify MainActivity.kt


package com.microappvalley.androidpdfviewerurlandassetfolderkotlinexample

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.github.barteksc.pdfviewer.PDFView

class MainActivity : AppCompatActivity() {
lateinit var pdfView: PDFView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

pdfView = findViewById(R.id.pdfView)

pdfView.fromAsset("demo.pdf").load()


}
}


6/ Create an assets folder under the main folder and paste the demo.pdf file in the assets folder


Comments