How to convert JPG image file into PDF file in your Android App? – complete source code.

This video shows the steps to develop your App in Android Studio environment to convert the JPG file into PDF file format. The steps are very simple. In this App it is shown that it first reads the JPG file in your App and then converts it into Bitmap. Subsequently it creates a PDF file and then draws the bitmap image in it’s page canvas. And finally saves the pdf document to the specified PDF file.

The JPG image/ picture file used in this video has been created using myCamera App. The steps to create it is shown in the below YouTube video and is recommended if interested to know the steps to create the jpg file: https://youtu.be/i51wFi5flqw

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

Source Code:

package com.example.myjpgtopdf;

import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.pdf.PdfDocument;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

public class MainActivity extends AppCompatActivity {

private String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + “/myCamera/”;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(this, new String[]{WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
}

public void convertButton(View view){

String file = directory + “3.jpg”;
Bitmap bitmap = BitmapFactory.decodeFile(file);

PdfDocument pdfDocument = new PdfDocument();
PdfDocument.PageInfo myPageInfo = new PdfDocument.PageInfo.Builder(960,1280,1).create();
PdfDocument.Page page = pdfDocument.startPage(myPageInfo);

page.getCanvas().drawBitmap(bitmap,0,0, null);
pdfDocument.finishPage(page);

String pdfFile = directory + “/myPDFFile_3.pdf”;
File myPDFFile = new File(pdfFile);

try {
pdfDocument.writeTo(new FileOutputStream(myPDFFile));
} catch (IOException e) {
e.printStackTrace();
}

pdfDocument.close();

}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.myjpgtopdf”>

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”100dp”
android:layout_marginTop=”250dp”
android:onClick=”convertButton”
android:text=”@string/convert_jpg_to_pdf” />
</RelativeLayout>

10 comments

    • Try to add multiple pages as below and keep printing the images into the pages.

      // 1st Page
      PdfDocument.PageInfo myPageInfo1 = new PdfDocument.PageInfo.Builder(960,1280,1).create();
      PdfDocument.Page page1 = pdfDocument.startPage(myPageInfo1);
      page1.getCanvas().drawBitmap(bitmap1,0,0, null);
      pdfDocument.finishPage(page1);

      // 2nd Page
      PdfDocument.PageInfo myPageInfo2 = new PdfDocument.PageInfo.Builder(960,1280,2).create();
      PdfDocument.Page page2 = pdfDocument.startPage(myPageInfo2);
      page2.getCanvas().drawBitmap(bitmap2,0,0, null);
      pdfDocument.finishPage(page2);

      // … so on for further pages.

      It should work.

      Cheers
      Programmer World

  1. sir how to do e signature with pdf downloadable document with e signature using canvas, i have getting pdf from my internal storage and adding into that signature pad libarary after that i written on the pdf but when i click save button , the canvas image only downloaded as a image. But i want pdf and what i have written of canvas word as a pdf document. please can you help me

    • Concept is very simple. You have to open your image in bitmap in your code. Then use that bitmap in your canvas. And then use that canvas to write on the page (of PDF). It should work.

      If you have done that and still results are not as expected then please copy your java code here. It will help.

      Cheers
      Programmer World
      https://programmerworld.co

Leave a Reply