How to Open PDF file in Android Programmatically using Android Studio. To open a PDF File in Android Application, your app can take help from Free Android library available on Github.

Need some career advice or prepping for an Android developer interview? Hit me up on, and let's chat!

In this tutorial series, we will teach you how to open a pdf file in the Android programmatically included with the help of Android PDFViewer library developed by Barteksc.

By using Android Library you are not only leveraging the hard work from another developer but also making it easier and quicker to develop an android app.

Before I proceed further, here is the screenshot of the kind of PDF Viewer that’ll be created using Android Studio.

How to open PDF file in Android Programmatically
How to open PDF file in Android Programmatically

How To Open PDF File in Android Programatically

The app will open a specified pdf file which will be copied in the assets folder of your Android studio project. I’ve used bitcoin.pdf file, you can use any of your choices.

Software Files used:

  1. Android Studio (Version 3.x)
  2. Any 1 PDF File

Files Edited / Created:

  1. activity_main.xml
  2. MainActivity.Java
  3. Build.Gradle (app)

Here are the Steps you need to follow on ‘How To Open PDF File in Android Programmatically’:

1.Open Android Studio and create a new project with Application name as, ‘FIMC PDFViewer’ (you can use any of your choices!).

2. We have used API level 19 in our project as 90% devices support this, click next and select to create an Empty Activity.

3. Once Gradle build is completed, go to ‘Gradle Scripts’ in the left side panel of Project Window. Search for ‘build.gradle (app)

4. Search for dependencies and add the following line of code to it.

dependencies {
    //using stable version
    implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

5. To make sure that you\’re using the updated version of PDFViewer library, I strongly recommend visiting source code on GitHub

6. Now check for a Yellow color bar on top of Project Window and click on ‘Build Gradle’.

7. Now go to activity_main.xml file in res > layout in Text mode and remove any component other than the layout.

Now add following lines of code to it:

   android:layout_height="match_parent" /> 

8. Go back to Project panel in the left of Android Studio and right click on the ‘app’ folder.

9. Go to New Folder > New Assets Folder. Click OK. The New assets folder is now created.

10. Right-click on the \’assets\’ folder and select, ‘Open in Explorer’.

11. Copy and Paste your PDF File in this folder.

12. Go and open MainActivity.Java file in java folder. Using findViewByID function link the PDFView component and then link it to the pdf file in assets folder using the code as shown below:

PDFView pdfView = (PDFView) findViewById(;

Now that your own personal PDFViewer App is ready, Build and run in on your Device.

Exploring other options

In the example above we have opened the pdf file from the internal storage. But you can also open a pdf file from a URL link, file link in external or internal storage or even stream an online file using the code below:

// Open a PDF file from given URL

// Open a PDF file from given File link

// Open a PDF file using Bytes

// Open a PDF file using INput Stream
// stream is written to bytearray - native code cannot use Java Streams

// Open a PDF file from DocumentSource

Using Attributes

In this blog you not only learn how to Open PDF File in Android Programmatically but also we have explored few other attributes provided in this library.

   .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
    .enableSwipe(true) // allows to block changing pages using swipe
    .swipeHorizontal(false) // to enable page swiping
    .enableDoubletap(true) // to enable double tap
    .defaultPage(0) // default page. this can be used to save user last page location

    // allows to draw something on the current page, usually visible in the middle of the screen
    // allows to draw something on all pages, separately for every page. Called only for visible pages
    .onLoad(onLoadCompleteListener) // called after document is loaded and starts to be rendered
    .onRender(onRenderListener) // called after document is rendered for the first time
    // called on single tap, return true if handled, false to toggle scroll handle visibility
    .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
    .enableAntialiasing(true) // improve rendering a little bit on low-res screens
    // spacing between pages in dp. To define spacing color, set view background
    .autoSpacing(false) // add dynamic spacing to fit each page on its own on the screen
    .pageSnap(true) // snap pages to screen boundaries
    .pageFling(false) // make a fling change only a single page like ViewPager
    .nightMode(false) // toggle night mode

Customise and Run your app, any feedback below will be appreciated 🙂

1 comment
Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like