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 MainActivity.java

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

      Hi,
      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

    content.getWidth()
    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(Read_pdf_main.java:249)
    at com.micron.jrobinsonriv.read_pdf.Read_pdf_main.onClick(Read_pdf_main.java:275)
    at android.view.View.performClick(View.java:5702)
    at android.widget.TextView.performClick(TextView.java:10887)
    at android.view.View$PerformClick.run(View.java:22546)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7237)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

  • 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?