An Android App for Converting Images to PDF

Let’s now start a new subseries of articles for converting JPG, PNG, GIF and TIFF images into PDF documents. Are you ready to develop a new fully functional Android app?

An Android App for Converting Images to PDF

At this moment you should feel comfortable with creating new Android Studio projects from scratch as well as building simple PlugPDF applications, so please be encouraged to copy the code snippets below and paste them on your Android apps.

Once again, since we are proceeding in steps, in this first part we’ll focus on the Java logic that performs the conversion. The whole application’s code will be attached a little later.

Running your own tests and playing with the Java code will help you to better understand our PDF API for Android.

Putting an Image on an A4 Paper

The simplest thing to start with is to put an image on a document, so let’s take a blank A4 sheet of paper and place the image selected by the user in:

  • The upper left corner
  • The upper right corner
  • The center of the page
  • The bottom left corner
  • The bottom right corner

The following code snippet is a simple framework that enables to achieve our goal.

Now let us take a closer look at the commented lines number 16 and 17.

You’ll notice that line number 17 must be updated every time you want to put an image in a different corner:

Figure 1. The upper left corner

Figure 1. Upper left corner

Figure 2. Upper right corner

Figure 2. Upper right corner

Figure 3. Center of the page

Figure 3. Center of the page

Figure 4. The bottom left corner

Figure 4. Bottom left

Figure 5. The bottom right corner

Figure 5. Bottom right

It is quite simple, right? We’ve just placed a blank PDF page called blank_a4.pdf on the main\app\src\main\assets folder. Then, the readAssetFile(String) method, which you should already recognize from previous examples, is used again to convert the file into an array of bytes.

And finally, all the magic takes place in the ReaderView object. Please, see the insertImageWatermark method for more details.

0 replies

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 *