Kotlin PDF Viewer Library Example

Welcome folks I am back with another blog post. Today in this post we will be looking at a library which allows you to embed PDF Documents in Kotlin Application. The library name is PdfView-Android. You can download the library from here:

  1. Download

Features

  1. It can handle big files to embed in Android Application
  2. It is based on ImageView Element in Android
  3. It gives you ultimate performance in rendering complex and big size PDF Documents.
See also  Kotlin Google Maps AutocompleteTextView Location Example

 

Demo

 

 

How to Use

 

In order to use this library you need a PDF File on the system. Then you can write this line of code to include it through this library and display the file in app.

See also  Kotlin(Android) Typing Indicator or Wait Progress Animation

 

findViewById<PDFView>(R.id.activityMainPdfView).fromAsset("paper.pdf").show()

 

Add to your project:

repositories {
        <...>
        jcenter()
    }

 

Add gradle dependency:

 

implementation "com.pdfview:pdfview-android:1.0.0"

Full Example Code

 

  1. Layout File:

 

activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.pdfview.PDFView
        android:id="@+id/activityMainPdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

 

See also  Kotlin RecyclerView Animation Example

2) Java Code:

 

MainActivity.kt

 

package com.pdfview_sample.pdfview

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.pdfview.PDFView

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<PDFView>(R.id.activityMainPdfView).fromAsset("great-expectations.pdf").show()
//        findViewById<PDFView>(R.id.activityMainPdfView).fromAsset("paper.pdf").show()
    }
}

Leave a Reply