Create and Display PDF within Android Application

Embedding functionality to Create and Display PDF within Android Application can increase engagement of your user significantly. Rather than asking user to bounce off to other pdf viewer application, we will create an application with support of Pdf creation and viewing functionalities.



Sample Application For Creation and Display of Pdf

1. Create a new Android Application Project with Package name com.androidsrc.pdfdemo. Make sure to select API 21. As PdfRenderer was introduced in requires API level 21. If you are creating functionality of Pdf document creation only, you can opt for API 19 as well.

2. Preparing AndroidManifest.xml

Add permission into your manifest file to read and write from external storage. Ensure to add uses-sdk tag with minSdkVersion specified as 21.

3. Preparing UI Components

For our main view, we will use FrameLayout as parent view. It will have four child LinearLayout views for displaying pdf options, pdf document listing and reading pdf, creating pdf. We will navigate between these views intercepting back key and close option in menu. Final activity_main.xml will be as below after including views for above functionalities.

We will be using ListView for displaying list of Pdf document available. Create new layout file /res/layout/list_item.xml for list viw item. It will just have TextView which will display path for Pdf documents.

Create new menu file /res/menu/main.xml. It will have one menu item which will incorporate functionality to navigate between different views in layout.

4. Preparing Applicaiton Launcher Activity Source Code

Creating PDF Document

For creating PDF Document, we will use PdfDocument class which was added in API level 19. It prvoides api to create new PDF document, you can process content creation page by page using PdfDocument.Page. PDF page associates a canvas on which you can draw your content. For representing attributes of a page like height, width and page number etc PdfDocument.PageInfo is used.

Pretty cool, with just few lines of codes as above you can create new Pdf document. You can simply provide your user to share content of your application in form of Pdf document. For applications like billing, patient information etc, user can quickly opt to create Pdf and share.

Displaying PDF Document

For displaying PDF Document, we will use PdfRenderer class which was added in API level 21. With this you can render Pdf Document pages into native views of your android application. PdfRenderer.Page is used to open, render and closing page of particular index in Pdf document.

Complete code for

5.Buid and Run your Application

We are done. Build and run your application, you can read and create Pdf document within your application.

You may also like...

  • Asad Mughal

    its really a good tutorial

  • Sunil Singh

    can I add textcolor and textstyle in PDF using PDFviewer pls help

  • daniyal

    it is showing errors

    • Lex

      DId you ever get a response for this? I have the same issue.

      • try to call it inside runOnUiThread();

        • Rando S Putra

          Please fix the code ^_^. Newbie like me cannot found the runOnUiThread() 😛

    • try to call it inside runOnUiThread.

  • Pranav Pal Lekhi

    The line ‘outputFile.createNewFile();’ is giving me an IOException. It says ‘ENOENT No such file or directory’. What could I be doing wrong?

    • Mohammad Nasser

      you habe to create the folder before using it

  • Prachi Singh

    Hi, thanks a lot for your help! Can you tell me how to zoom-in or zoom-out while displaying a pdf?

  • Masi Ahmad

    Hi i also get the errors

    and content.draw(page.getCanvas());

    And idont know how to call runOnUiThread

  • Pingback: Displaying a PDF Document inside a WebView – SAIFUL DIARY()

  • Jon

    Hi does anyone get a “NullPointerException” on closeOption?

    10-08 16:06:23.001 30249-30249/com.micron.jrobinsonriv.read_pdf E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.micron.jrobinsonriv.read_pdf, PID: 30249
    java.lang.NullPointerException: Attempt to invoke interface method ‘android.view.MenuItem android.view.MenuItem.setVisible(boolean)’ on a null object reference
    at com.micron.jrobinsonriv.read_pdf.Read_pdf_main.updateView(
    at com.micron.jrobinsonriv.read_pdf.Read_pdf_main.onClick(
    at android.view.View.performClick(
    at android.widget.TextView.performClick(
    at android.view.View$
    at android.os.Handler.handleCallback(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)

  • Carlos Sierra

    how to use this when your app uses only a webview ( I develop hybrid apps, using html content local storage )

  • ani

    Thank you so much for the sharing. I’m having an issue when trying to put a message dialog. It needs to click “OK” button several times(~2-3 times) to close the dialog.

    • ani

      I made mistake with my progress dialog which causes the need of several times click. Not because of your code. Hehe Thanks

  • Mica Kardashian

    Como faço pra ler pdf com uma api abaixo da 21?
    quero na versão 4.0

  • Pavneet Singh

    when i click the botton “open pdf” and “create pdf “application is crash !!
    Give me solution…

  • Abdul Rehman

    its has many errors update the code and in how much android app api till it works?