Create pdf reader app for Android

 Hi guys welcome to mr ethical Yt. Are you lazy to read PDFs or books(PDF) like me. So let's create a android app that reads books(PDF) and PDFs for us.


First create a android project.

Add these two dependencies in build.gradle.

implementation 'com.itextpdf:itextg:5.5.10'

 implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'

Copy the pdf file to Assets folder and res/raw folder.

Copy the below xml code and paste in activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical">

	<android.support.design.widget.AppBarLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

		<android.support.v7.widget.Toolbar
			android:id="@+id/toolbar"
			android:layout_width="match_parent"
			android:layout_height="?attr/actionBarSize"
			app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

	</android.support.design.widget.AppBarLayout>
	<Button
		android:layout_width="wrap_content"
		android:id="@+id/activity_mainButton"
		android:layout_height="35dp"
		android:background="#FF0024D2"
		android:textColor="#ffffff"
		android:layout_gravity="center_horizontal"
		android:text="START"/>
	<com.github.barteksc.pdfviewer.PDFView
		android:id="@+id/pdfView"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
	/>
</LinearLayout>
Copy the below java code and paste in MAINACTIVITY.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.github.barteksc.pdfviewer.PDFView;
import android.graphics.pdf.PdfRenderer;
import android.os.ParcelFileDescriptor;
import java.io.IOException;
import com.github.barteksc.pdfviewer.util.FitPolicy;
import com.github.barteksc.pdfviewer.PdfFile;
import com.github.barteksc.pdfviewer.listener.OnPageScrollListener;
import android.speech.tts.TextToSpeech;
import java.util.Locale;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import android.widget.Button;
import android.view.View;
import android.util.Log;
import java.io.File;
import android.os.Environment;
import java.io.FileWriter;
import android.widget.Toast;
import android.widget.TextView;
import android.content.Intent;
import android.net.Uri;
import com.github.barteksc.pdfviewer.source.DocumentSource;

public class MainActivity extends AppCompatActivity {
	TextToSpeech textToSpeech;
	String text;
	int code=27;
	
	PDFView pdfView;
	
	Button speak;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
		pdfView = findViewById(R.id.pdfView);
	    speak=findViewById(R.id.activity_mainButton);	
		Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
		setSupportActionBar(toolbar);
		textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { 
				@Override
				public void onInit(int i) { 
					// if No error is found then only it will run 
					if(i!=TextToSpeech.ERROR){  
						// To Choose language of speech 
						textToSpeech.setLanguage(Locale.UK);  
					} 
				} 
			});
		DocumentSource source = null;
		pdfView.fromAsset("");
		
		pdfView.fromAsset("Pdf.pdf")
			.swipeHorizontal(true)
			.enableDoubletap(true)
			.defaultPage(0)
			.enableAnnotationRendering(false)
			.password(null)
			.scrollHandle(null)
			.enableAntialiasing(true)
			.spacing(0)
			.pageFitPolicy(FitPolicy.WIDTH)
			.load();
		speak.setOnClickListener(new View.OnClickListener(){
				@Override
				public void onClick(View p1) {
					try {
						PdfReader reader=new PdfReader("res/raw/Pdf.pdf");
						if(!textToSpeech.isSpeaking()){	
						speak.setText("STOP");
							text=PdfTextExtractor.getTextFromPage(reader,pdfView.getCurrentPage()+1);
							textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null);
						}else{
							textToSpeech.stop();
							speak.setText("START");
						}
					} catch (IOException e) {}
				}
			});
	}
}

Build the apk.


Post a Comment (0)
Previous Post Next Post