Android Studio

Android Studio PDF Viewer From URL Or Assets And Multiple

Android Studio PDF Viewer From URL প্রিয় বন্ধুগণ অ্যান্ড্রয়েড অ্যাপ এ ওয়েবসাইটের পিডিএফ কিভাবে শো করানো হয়। এন্ড্রয়েড এপস এর পিডিএফ অ্যাড করে কিভাবে শো করানো হয় বিস্তারিত থাকছে।

android-pdf viewer library from url

android-pdf viewer library from url
android-pdf viewer library from url

এই PDF ভিউ যোগ করার জন্য আমরা একটি লাইব্রেরি ব্যবহার করছি যা আমাদের URL থেকে PDF লোড করতে সাহায্য করবে। মনে রাখবেন যে আমরা জাভা ভাষা ব্যবহার করে এই প্রকল্পটি বাস্তবায়ন করতে যাচ্ছি।

pdf viewer android studio example

pdf viewer android studio example
pdf viewer android studio example

প্রথমে অ্যান্ড্রয়েড স্টুডিও তে নতুন একটা প্রজেক্ট তৈরি করুন এবার অ্যান্ড্রয়েড স্টুডিও তে লাইব্রেরী এড করে লিংক করে নিন।

Step 1: Create a New Project 

Step 2: Add to settings.gradle(settings.project)

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()

        jcenter()

        mavenCentral()
    }
}

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.

       implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

Add below two lines inside your AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

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.

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


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


</RelativeLayout>

Navigate to the app > java > your apps package name > MainActivity.java file. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;

import com.github.barteksc.pdfviewer.PDFView;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends AppCompatActivity {

    PDFView pdfView;
    String pdfurl = "https://www.w3.org/WAI/ER/tests/xhtml/testfies/resources/pdf/dummy.pdf";

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

        //initializing our pdf view.
        pdfView = findViewById(R.id.pdfView);
        new RetrivePDFfromUrl().execute(pdfurl);

    }

    //create an async task class for loading pdf file from URL.
    class RetrivePDFfromUrl extends AsyncTask<String, Void, InputStream> {

        @Override
        protected InputStream doInBackground(String... strings) {
            //we are using inputstream for getting out PDF.
            InputStream inputStream = null;
            try {
                URL url = new URL(strings[0]);
                // below is the step where we are creating our connection.
                HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                if (urlConnection.getResponseCode() == 200) {
                    //response is success.
                    //we are getting input stream from url and storing it in our variable.
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());

                }

            } catch (IOException e) {
                //this is the method to handle errors.
                e.printStackTrace();
                return null;
            }

            return inputStream;
        }

        @Override
        protected void onPostExecute(InputStream inputStream) {
            // after the execution of our async task we are loading our pdf in our pdf view.
            pdfView.fromStream(inputStream).load();

        }
    }

}

Android Studio PDF Viewer

Android Studio PDF Viewer
Android Studio PDF Viewer

এই নিচের প্রোগ্রামিং এর মাধ্যমে অ্যান্ড্রয়েড অ্যাপের ভেতর থেকেই লোকাল পিডিএফ লোড করাতে পারেন জাস্ট এই নিচের প্রোগ্রামিং গুলো ফলো করুন।

                             pdfView.fromAsset(pdfname.pdf)
                            .password(null)
                            .defaultPage(0)
                            .enableSwipe(true)
                            .swipeHorizontal(false)
                            .spacing(0)
                            .load();}

C:\Users\info\Desktop\a\app\src\main\assets\pdfname.pdf

এই লোকেশনে একটি ফোল্ডার তৈরী করুন এবং আপনার কাংখিত পিডিএফ ফাইলটি এখানে রাখুন কিংবা অন্য নামে হলে মেইন অ্যাক্টিভিটি থেকে পিডিএফ নামটি চেঞ্জ করে দিন।

Android Studio PDF Viewer From URL

Android Studio PDF Viewer From URL
Android Studio PDF Viewer From URL

আপনি যদি বাটন এর সাহায্যে মাল্টিপল পিডিএফ লোড করাতে চান তাহলে এই পদ্ধতি অনুসরণ করুন এই নিচের প্রোগ্রামিং গুলো রিপ্লেস করুন।

pdf viewer in android studio source code

pdf viewer in android studio source code
pdf viewer in android studio source code

অ্যান্ড্রয়েড স্টুডিও সোর্সকোড ডাউনলোড করার জন্য নিচে দেখুন বাংলায় লেখা ইনস্টল করুন এখানে ক্লিক করলেই স্বয়ংক্রিয়ভাবে ডাউনলোড হয়ে যাবে এবার আনজিপ করে অ্যান্ড্রয়েড স্টুডিও করে নিন

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button *