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:

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();

}
}…(read more)


Convert Word 2 PDF: Word to PDF Converter

Convert PowerPoint 2 PDF: PPT to PDF Converter

Convert Excel 2 PDF: Excel to PDF Converter

Convert an Image 2 PDF: Image to PDF Converter

Convert HTML 2 PDF: HTML to PDF Converter

More Tools: PDF Converter