Recently Added

6/recent/ticker-posts

How to Convert Image to PDF in Android Studio Part 4

In this video, I a going to show you how to read pdf in your app
So let's start.

1.add this  Dependency to your build.gradle file 2nd one 

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

2. also update your code in your build.gradle 1st one
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"

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

allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
maven { url 'https://www.jitpack.io' }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
3. In case if you get an error like Duplicate class or Manifest merge,
open your gradle.properties and add this two line of code

android.useAndroidX=true
android.enableJetifier=true

4. Open your FileAdapter.java add this code inside OnBindViewHolder


holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, PDFViewActivity.class);
intent.putExtra("position", position);
context.startActivity(intent);
}
});


5. Now Create an Empty Activity And name it like PDFViewActivity and open activity_pdfview.xml and add this code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PDFViewActivity">

<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

6.open your PDFViewActivity.java and add this code

public class PDFViewActivity extends AppCompatActivity {


int position = -1;
PDFView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdfview);

position = getIntent().getIntExtra("position", -1);

pdfView = findViewById(R.id.pdfView);

displayPDF();
}

private void displayPDF(){
String path = pdfAdapterArray.get(position).getPath();
File file = new File(path);
pdfView.fromFile(file)
.defaultPage(0)
.enableSwipe(true)
.enableDoubletap(true)
.load();

}
}
Watch full Tutorials

Post a Comment

0 Comments