Load PDF from Internet in Android using Barteksc

Barteksc Android PdfViewer Online Pdf View

Hello, In this Artical i will show you how to show pdf show in your android app from Internet. Very Easy Step to do this.

Library :

Build.gradle
   //PdfViewer  
   implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'  

Add maven in gradle.settings

jcenter()
maven { url 'https://jitpack.io' }  

Add Jetifier in gradle.properties

 android.enableJetifier=true  

Internet Permission :

Manifests

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

Create A New Empty Activity For PDF Setup Viewer to easyly Access PDF Viewer from another activity

XML Layout :

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

PDF Activiy Java Code :

 // Setup Veriable in Public Class ==============  
 PDFView pdfView;  
 ProgressBar progressBar;  
   
   
 // Add This In OnCreate Method ==============  
  pdfView = findViewById(R.id.pdfView);  
     progressBar = findViewById(R.id.progressBar);  
     Intent intent = getIntent();  
     String PDFURL = intent.getStringExtra("pdflink");  
     new RetrivePDFfromUrl().execute(PDFURL);  
   
   
 // Create Public Class in this Activity  
   private class RetrivePDFfromUrl extends AsyncTask<String, Void, InputStream> {  
     @Override  
     protected InputStream doInBackground(String... strings) {  
       InputStream inputStream = null;  
       try {  
         URL url = new URL(strings[0]);  
         HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();  
         if (urlConnection.getResponseCode() == 200) {  
           inputStream = new BufferedInputStream(urlConnection.getInputStream());  
         }  
       } catch (IOException e) {  
         e.printStackTrace();  
         return null;  
       }  
       return inputStream;  
     }  
   
     @Override  
     protected void onPostExecute(InputStream inputStream) {  
   
       pdfView.fromStream(inputStream)  
           .enableSwipe(true)  
           .swipeHorizontal(true)  
           .enableDoubletap(true)  
           .defaultPage(0)  
           .enableAnnotationRendering(false)  
           .password(null)  
           .scrollHandle(null)  
           .enableAntialiasing(true)  
           .spacing(0)  
           .pageFitPolicy(FitPolicy.WIDTH)  
   
           .pageSnap(true) // snap pages to screen boundaries  
           .pageFling(true) // make a fling change only a single page like ViewPager  
   
           .onLoad(new OnLoadCompleteListener() {  
             @Override  
             public void loadComplete(int nbPages) {  
               progressBar.setVisibility(View.GONE);  
             }  
           })  
           .load();  
     }  
   
   }  

Another Activity Button Click to Send Pdf URL :

   Intent intent = new Intent(this, PdfViewer.class);  
           intent.putExtra("serverURL", Constants.CHEMISTRY);  
           startActivity(intent);  

Next Post Previous Post
1 Comments
  • mrinal
    mrinal August 26, 2023 at 8:26 PM

    Vaiya pdf view Library use korle app size 40-50 mb hoye jay. apnake sob jayga te ai question ta korcilam kintu apni kono ans den ni. Asa kori akta solution diben. kivabe app size komate pari. Ans er opekkhay thaklam

Add Comment
comment url