Extract Data From PDF File in Android using Kotlin
Last Updated :
19 Jun, 2022
PDF is a portable document format that is used to represent data such as images, tables, and many more. Nowadays the use of PDF is increased rapidly in different fields. Many apps have switched to overusing PDF files to represent data. So some of the apps have a requirement to extract the data from the PDF file and display that data inside our app. In this article, we will create an application to extract the data from the PDF file and display it in our app using Kotlin.
Note: If you are looking to implement How to extract data from PDF files in Android using Java. Check out the following article: How to extract data from PDF in Android using Java
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Add dependency to the build.gradle(Module:app)
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
implementation 'com.itextpdf:itextg:5.5.10'
Now sync your project to install it.
Step 3: Adding a PDF file to your app
As we are extracting data from PDF files, so we will be adding PDF files to our app. For adding PDF files to your app we have to create the raw folder first. Please refer to Resource Raw Folder in Android Studio to create a raw folder in android. After creating a new raw directory copy and paste your PDF file inside that “raw” folder. After adding that PDF file to your app, now we will move towards implementation in the XML part.
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--on below line we are creating
a text for heading of our app-->
<TextView
android:id="@+id/idTVExtracter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:gravity="center"
android:padding="4dp"
android:text="Text Extracter from PDF"
android:textAlignment="center"
android:textColor="@color/purple_200"
android:textSize="18sp"
android:textStyle="bold" />
<!--on below line we are creating a scroll view for our text-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/idBtnExtract"
android:layout_below="@id/idTVExtracter">
<!--text view for displaying our extracted text-->
<TextView
android:id="@+id/idTVPDF"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
<!--button for starting extraction process-->
<Button
android:id="@+id/idBtnExtract"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:text="Extract Text from PDF"
android:textAllCaps="false" />
</RelativeLayout>
Step 5: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.itextpdf.text.pdf.PdfReader
import com.itextpdf.text.pdf.parser.PdfTextExtractor
class MainActivity : AppCompatActivity() {
// on below line we are creating
// variable for our button and text view.
lateinit var extractedTV: TextView
lateinit var extractBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// on below line we are initializing our
// text view and button with its id.
extractedTV = findViewById(R.id.idTVPDF)
extractBtn = findViewById(R.id.idBtnExtract)
// on below line we are adding on
// click listener for our button.
extractBtn.setOnClickListener {
// on below line we are calling extract data method
// to extract data from our pdf file and
// display it in text view.
extractData()
}
}
// on below line we are creating an
// extract data method to extract our data.
private fun extractData() {
// on below line we are running a try and catch block
// to handle extract data operation.
try {
// on below line we are creating a
// variable for storing our extracted text
var extractedText = ""
// on below line we are creating a
// variable for our pdf extracter.
val pdfReader: PdfReader = PdfReader("res/raw/android.pdf")
// on below line we are creating
// a variable for pages of our pdf.
val n = pdfReader.numberOfPages
// on below line we are running a for loop.
for (i in 0 until n) {
// on below line we are appending
// our data to extracted
// text from our pdf file using pdf reader.
extractedText =
"""
$extractedText${
PdfTextExtractor.getTextFromPage(pdfReader, i + 1).trim { it <= ' ' }
}
""".trimIndent()
// to extract the PDF content from the different pages
}
// on below line we are setting
// extracted text to our text view.
extractedTV.setText(extractedText)
// on below line we are
// closing our pdf reader.
pdfReader.close()
}
// on below line we are handling our
// exception using catch block
catch (e: Exception) {
e.printStackTrace()
}
}
}
Now run your application to see the output of it.
Output:
Similar Reads
How to Extract Data from PDF file in Android?
PDF is a portable document format that is used to represent data such as images, tables, and many more. Nowadays the use of PDF is increased rapidly in different fields. Many apps have switched overusing PDF files to represent data. So some of the apps have a requirement to extract the data from the
4 min read
Extract Data From PDF File using Android Jetpack Compose
PDF files are used in many android applications for displaying data in the form of images, text as well as graphics. Many applications are also required to get the data from this PDF file and display this data within the android application. So for extracting the data from PDF file pdf reader is use
3 min read
Generate PDF File in Android using Kotlin
Most of the applications provide users with the facility of bills to be downloaded from the mobile applications. This type of application gets the data from the APIS or data within the application and this data is used to generate the PDF files. PDF files within android are generated using canvas. I
7 min read
Generate QR Code in Android using Kotlin
Many applications nowadays use QR codes within their application to hide some information. QR codes are seen within many payment applications which are used by the users to scan and make payments. The QR codes which are seen within these applications are generated inside the application itself. In t
4 min read
Play Audio From URL in Android using Kotlin
Many applications want to add different types of audio files to their android applications. These audio files are played using a media player within the android application. We can play audio files within the android application from different sources by playing audio from a web URL or by simply add
4 min read
Android - Extract Data From JSON Array using Retrofit Library with Kotlin
In the android application, the response from the server is in the form of JSON. We can get to see either a JSON Array or a JSON Object in the response. JSON Array is the list of data which is having similar JSON objects. In the previous article, we have taken a look at How to parse data from JSON O
7 min read
Android - Extract Data From JSON Array using Volley Library with Kotlin
JSON responses are of 2 types i.e. JSON Object and JSON Array. JSON Array consists of individual JSON objects which are having same similar structure but have different values within it. JSON Array is used to parse the data in the form of a list or an array. In the previous article, we have taken a
7 min read
Read From Files using BufferedReader in Kotlin
In Kotlin, BufferedReader stores some characters as it reads into the buffer. This makes the reading faster and hence more efficient. In this article, we will understand how to use the BufferedReader to read the contents of a file. Note: To Read From Files using InputReader please refer to Read Fro
4 min read
Kotlin Flow in Android with Example
Kotlin Flow is a tool that helps developers work with data that changes over time like search results, live updates, or user input. Itâs part of Kotlinâs coroutines, which are used for writing code that doesnât block the app while waiting for something to finish, like a network call or a file to loa
8 min read
Text Detector in Android using Firebase ML Kit
Nowadays many apps using Machine Learning inside their apps to make most of the tasks easier. We have seen many apps that detect text from any image. This image may include number plates, images, and many more. In this article, we will take a look at the implementation of Text Detector in Android us
6 min read