How to create your PDF reader Android App? – complete source code

This video shows simple steps to create your own PDF Reader Android App. You can create your own PDF reader App which will read out the text in any PDF file. This App uses TextToSpeech method to convert the text to speech and speak out in the locale language you have set in your App.

To watch and understand the TextToSpeech command please watch my Youtube video at: https://youtu.be/zt6VXJmy-p0

To read the PDF it uses PdfReader and PdfTextExtractor which are a part of itextpdf package. So, please ensure to have the following line of code in your gradle and sync the gradle to have the package available for your in your App code.
implementation ‘com.itextpdf:itextg:5.5.10’

To get the steps to create a PDF file from your App, please refer to the below video: https://youtu.be/RjpFwkfRM3U
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com


The complete source code is being pasted below:

package com.example.mypdfreader;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.TextView;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

private TextView textView;
private TextToSpeech textToSpeech;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView);

textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
textToSpeech.setLanguage(Locale.US);
}
});

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
}

public void readButtonOnClick(View view){
File file = new File(“/sdcard/myPDFFile.pdf”);

String stringParser;

try {
PdfReader pdfReader = new PdfReader(file.getPath());
stringParser = PdfTextExtractor.getTextFromPage(pdfReader, 1).trim();
pdfReader.close();
textView.setText(stringParser);
textToSpeech.speak(stringParser, TextToSpeech.QUEUE_FLUSH,null, null);
} catch (IOException e) {
e.printStackTrace();
}
}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.mypdfreader”>

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
<uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE”/>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<TextView
android:id=”@+id/textView”
android:layout_width=”250dp”
android:layout_height=”wrap_content”
android:layout_marginStart=”100dp”
android:layout_marginTop=”150dp”
android:text=”Hello World!”
android:textSize=”30sp”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />

<Button
android:id=”@+id/readButton”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”150dp”
android:layout_marginTop=”50dp”
android:onClick=”readButtonOnClick”
android:text=”@string/read” />

</RelativeLayout>

Gradle (App) file

apply plugin: ‘com.android.application’

android {
compileSdkVersion 29
defaultConfig {
applicationId “com.example.mypdfreader”
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName “1.0”
testInstrumentationRunner “androidx.test.runner.AndroidJUnitRunner”
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’
}
}
}

dependencies {
implementation fileTree(dir: ‘libs’, include: [‘*.jar’])
implementation ‘androidx.appcompat:appcompat:1.0.2’
implementation ‘androidx.constraintlayout:constraintlayout:1.1.3’
testImplementation ‘junit:junit:4.12’
androidTestImplementation ‘androidx.test:runner:1.2.0’
androidTestImplementation ‘androidx.test.espresso:espresso-core:3.2.0’
implementation ‘com.itextpdf:itextg:5.5.10’
}

Gradle (Project) file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
google()
jcenter()

}
dependencies {
classpath ‘com.android.tools.build:gradle:3.4.2’

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()

}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

2 comments

  1. sir, i followed your code, but it appears “android.system.ErrnoException: open failed: EACCES (Permission denied)” and cant create the PDF

  2. The error which you are getting seems to be during file OPEN – as the exception says “open failed”.

    Please check you have given READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE to your App. You need to do this in both Java code and Manifest file.

    Once the above permissions are in place, the above exception should not be thrown.

    Just for your information, the above exception is being thrown most likely at the below line of code:
    PdfReader pdfReader = new PdfReader(file.getPath());

    Let me know if this doesn’t resolves your issue.

    Cheers
    Programmer World

Leave a Reply

%d bloggers like this: