My First Mobile PDF App

A classic tutorial for absolute beginner programmers is to write a Hello World application. Hello Worlds are certainly a great way to get started because they allow newbies to focus on the key concepts of the newest programming language, or the built-in components and features of a brand new SDK, just to mention a couple of examples.

My First Mobile PDF App

So today we are not going to be an exception. Now that you’ve been introduced to PlugPDF, let’s say hello to the world and write one of the simplest Android PDF apps.

Hello World!

The most uncomplicated mobile PDF app just opens a default PDF document. That’s it. More specifically, our basic PDF viewer when launched, automatically displays the eBook “Gone with the Wind” on the user’s device screen.

In this tutorial you will learn to build a mobile PDF app from scratch outside the context of the sample folder (as we already did in our previous article). We will also look at the ReaderView class, which is the core of this exercise.

Creating a New Android Project

As mentioned earlier, one of the aims of today’s article is to show how to create your first PDF app outside the context of the PlugPDF Sample Project, so let’s get started by creating a new Android Project.

Figure 1. Create a new Android Project

Figure 1. First of all click File > New > New Project…

Figure 2. The Create New Project wizard will start, whose first step is to fill the New Project dialog

Figure 2. The Create New Project wizard, whose first step is to fill the New Project dialog, will start

Figure 3. Continue with the Target Android Devices dialog

Figure 3. Continue with the Target Android Devices dialog

Notice that the minimum SDK required is API 13: Android 3.2 (Honeycomb)

Figure 4. Add a blank activity

Figure 4. Add a blank activity to your mobile app and then click on Next

Figure 5. Customize your activity as it is shown here

Figure 5. Finally, customize your activity as it is shown here, and click Finish

Figure 6. You should end up with this project structure

Figure 6. If everything is alright you should end up with this project structure

Importing the PlugPDF SDK

The next step is importing the PlugPDF SDK.

Figure 7. The next step is importing the PlugPDF SDK

Figure 7. File > New > Import Module…

Figure 8. Select the plugpdf source directory

Figure 8. Select the plugpdf source directory from your file system and click Next

Figure 9. Make sure that these checkboxes are selected

Figure 9. Make sure that the checkboxes are selected as it is shown in this figure and click Finish

Now wait a few seconds until the Gradle execution finishes. At the end of this process your Android Studio should look like the image below.

Figure 10. Hello World’s modules app and plugpdf

Figure 10. Hello World’s modules app and plugpdf

Fixing the Gradle Sync Issue

Once the Android project is sync with the Gradle files for the first time, Android Studio will warn: “The project encoding (windows-1252) does not match the encoding specified in the Gradle build files (UTF-8). This can lead to serious bugs.”

Figure 11. Fixing the Gradle sync warning

Figure 11. Fixing the Gradle sync warning

At this point the project default encoding must be changed from ANSI (Windows-1252) to UTF-8, so please click on Open File Encoding Settings and update to UTF-8 as illustrated by the next figure.

Figure 12. File Encodings

Figure 12. File Encodings

Customize the Main Activity

The Create New Project wizard has generated this MainActivity.java file for you.

Note that the default MainActivity class extends from ActionBarActivity, yet since this time we don’t need any Android navigation tool in order to code our simple PDF viewer, we’ll make the main class extend from Activity and remove the methods onCreateOptionsMenu and onOptionsItemSelected.

Finally, populate the onCreate method as shown below, and create the readAsset private method.

That’s all the Java code we need for our Hello World app!

Compiling

Compiling the plugpdf Module

Because the plugpdf module needs to be compiled, the PlugPDF SDK’s Java object types listed below will not be recognized at first:

Android Studio will show the notice “Cannot resolve symbol” regarding those types.

The compile project(':plugpdf') dependency must be added to the (Module: app) gradle script.

Finally click on Sync Project with Gradle Files and import the classes required by MainActivity:

Creating the Assets Folder

You are just a step away from running your first mobile PDF app! All you need to do now is copy the PlugPDF_Android\sample\assets folder and paste it into your AndroidStudioProjects\HelloWorld\app\src\main folder.

Figure 13. Copy and paste the assets folder

Figure 13. Copy and paste the assets folder

This enables the main activity’s readAssetFile method to load the PDF eBook “Gone With the Wind”.

Conclusion

In today’s post we’ve created an Android PDF app that basically loads a default PDF document stored on the assets folder. The end user only has to run this basic app in order to automatically open the eBook “Gone With the Wind”.

Figure 14. Our Hello World loads a default PDF document

Figure 14. Our Hello World loads a default PDF document

Once you get familiar with the steps involved in launching an app of this kind, this simple Hello World program allows you to focus on what really matters: The main activity’s Java code.

So today you have learned the following.

  • How to init the PlugPDF SDK from within the main activity’s onCreate method:
  • Read a PDF stored on the assets folder (Gone With the Wind):
  • Instantiate an object of type ReaderView (the PDF viewer):
  • Display a PDF eBook on the user’s device screen:
6 replies
  1. sais
    sais says:

    Hello,

    Ok, but what is the case for having three different pdf files?

    This instruction works ok only with one file:

    byte[] data = readAssetFile("Gone_With_the_Wind.pdf");

    But I’d like to read more than one.

    Thank you.

    Reply
  2. plugpdf
    plugpdf says:

    Hello sais,

    Firstly, close the PDF reader and call the mReader.clear(); method.

    Then, make a call to mReader.openData with some different data, from a different PDF file.

    I hope this will help.

    Reply
  3. ammar
    ammar says:

    i get an error here if (size > 0)
    gt cannot be resolved ,

    plus i get an error for the ” ; ” sign after gt , isn’t it supposed to be something like == or != ?

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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