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>

6 comments

      1. Can you highlight one or two errors you are observing?

        If you can copy the errors here then I can look into it.

        Cheers
        Programmer World

Leave a Reply

%d bloggers like this: