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
How to convert multiple image in PDF file pleases give this solution
sorry but my bitmap is null , and where did the getCanvas came from? the galley does not open
Sir, how to make a pdf file of multiple jpg images at a time?
You need to update this code because it will not work on the latest versions of android. This code (Environment.getExternalStoragePublicDirectory) will not work on latest versions.
Brother, thank you for the explanation
I did everything, but when I reduce the size of the image it loses clarity and becomes blurry
Is there a way to clarify the picture?
KOTLIN code please !
The image you set via Drawable. In PDF file
That's how to set the image via imageview. In PDF file
What is java coding
وہ تصویر جس کو آپ نے ڈرا ایبل کے ذریعے سیٹ کیا۔ پی ڈی ایف فائل میں
وہ ہی تصویرکو تصویرویو کے ذریعے سیٹ کرنے کا طریقہ کیا ہے۔ پی ڈی ایف فائل میں
How to add an image with hyperlink in pdf
Can you choose any picture without writing its name
Can I add a number of texts with the image?
wrote the exact steps but the app is crashing as soon as i press the button
it says "unfortunately has stopped working" …any solution?
Hello, is there a way to do this, but with an image that is selected from the gallery and not previously loaded on a route?
sir please make video on JPG to video or mp4
Thanks man means alot
Not working getExternalStorage ()