Open In App

Extract Data From PDF File using Android Jetpack Compose

Last Updated : 21 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 used which is used to read pdf and get the data from PDF file. In this article, we will be building a simple application in which we will be extracting data from PDF files in Android using Jetpack Compose

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. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Adding a new color in the Color.kt file

Navigate to the app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it.

Kotlin




package com.example.newcanaryproject.ui.theme
 
import androidx.compose.ui.graphics.Color
 
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
 
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)


Step 3: Adding dependency in build.gradle file 

Navigate to Gradle Scripts > build.gradle and add the below dependency in build.gradle file. 

implementation 'com.itextpdf:itextg:5.5.10'

After adding this dependency simply sync your project to install it. 

Step 4: Adding PDF file to your project

As we are extracting data from PDF files, so we will be adding PDF files to our app. To add PDF files to your app, we must 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.

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.example.newcanaryproject
 
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.example.newcanaryproject.ui.theme.*
import com.itextpdf.text.pdf.PdfReader
import com.itextpdf.text.pdf.parser.PdfTextExtractor
 
class MainActivity : ComponentActivity() {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NewCanaryProjectTheme {
                // on below line we are specifying
                // background color for our application
                Surface(
                    // on below line we are specifying
                    // modifier and color for our app
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
 
                    // on below line we are specifying the theme as scaffold.
                    Scaffold(
 
                        // in scaffold we are specifying top bar.
                        topBar = {
 
                            // inside top bar we are specifying background color.
                            TopAppBar(backgroundColor = greenColor,
 
                                // along with that we are specifying title for our top bar.
                                title = {
 
                                    // in the top bar we are specifying tile as a text
                                    Text(
 
                                        // on below line we are specifying
                                        // text to display in top app bar.
                                        text = "Text Extractor in Android",
 
                                        // on below line we are specifying
                                        // modifier to fill max width.
                                        modifier = Modifier.fillMaxWidth(),
 
                                        // on below line we are
                                        // specifying text alignment.
                                        textAlign = TextAlign.Center,
 
                                        // on below line we are
                                        // specifying color for our text.
                                        color = Color.White
                                    )
                                }
                            )
                        }
                    ) {
                        // on below line we are calling text extractor
                        // method to extract text from pdf.
                        textExtractor()
                    }
                }
            }
        }
    }
}
 
// on below line we are creating a text extractor
// method to extract text from pdf file.
@Composable
fun textExtractor() {
 
    // on below line we are creating
    // a variable for extracted text
    val extractedText = remember {
        mutableStateOf("")
    }
 
    // on below line we are creating a column for our ui.
    Column(
        // in this column we are adding a modifier
        // for our column and specifying
        // max width, height and size.
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .fillMaxSize()
 
            // on below line we are adding padding
            // from all sides to our column.
            .padding(6.dp),
 
        // on below line we are adding vertical
        // arrangement for our column as bottom
        verticalArrangement = Arrangement.Bottom,
 
        // on below line we are adding
        // horizontal alignment for our column.
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
 
        // on below line we are creating a
        // simple text for displaying our extracted text
        Text(text = extractedText.value, color = Color.Black, fontSize = 12.sp)
 
        // on below line we are adding a
        // spacer between a text and our button.
        Spacer(modifier = Modifier.height(10.dp))
 
        // on below line we are creating a button.
        Button(
            // on below line we are adding a modifier
            // to it and specifying max width to it.
            modifier = Modifier
                .fillMaxWidth()
 
                // on below line we are adding padding for our button.
                .padding(20.dp),
 
            // on below line we are adding on click for our button.
            onClick = {
 
                // inside on click we are calling extract
                // data method to extract data from our pdf file.
                extractData(extractedText)
 
            }) {
 
            // on the below line we are displaying a text for our button.
            Text(modifier = Modifier.padding(6.dp), text = "Extract Text from PDF")
        }
    }
 
}
 
// on below line we are creating an extract data method to extract our data.
private fun extractData(extractedString: MutableState<String>) {
    // 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 extractor.
        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.
        extractedString.value = 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: 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads