On click of app I had clicked image through camera and stored in sdcard now i want to convert it into pdf,I don't know how to do it.Can anyone help me. I wanted to acheive it on click of a button.is it possible
Asked
Active
Viewed 4,701 times
-2
-
Possible duplicate: http://stackoverflow.com/questions/15744454/how-to-convert-jpg-to-pdf-in-android-java – Manitoba Dec 09 '13 at 11:38
-
i tried using that way but was not able to implement it – poojagupta Dec 09 '13 at 11:40
-
What was the problem ? – Manitoba Dec 09 '13 at 11:40
-
12-09 11:43:49.737: I/System.out(25126): Exceptionjava.io.FileNotFoundException: /YourPDFHere.pdf: open failed: EROFS (Read-only file system) – poojagupta Dec 09 '13 at 11:44
-
Just a sec. I'm working on it. – Manitoba Dec 09 '13 at 11:52
-
no you can't do it on click of button, just on click of string – Mohammad Ersan Dec 09 '13 at 13:01
1 Answers
6
Here is a code that should do the job. I've tried to comment the maximum amount of lines, but if you don't understand, please tell me.
class JpgToPdfActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.jpg_to_pdf_activity);
// Get button
Button convertButton = (Button) findViewById(R.id.convert_button);
convertButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick()
{
// Will run the conversion in another thread to avoid the UI to be frozen
Thread t = new Thread() {
public void run()
{
// Input file
String inputPath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";
// Output file
String outputPath = Environment.getExternalStorageDirectory() + File.separator + "out.pdf";
// Run conversion
final boolean result = JpgToPdfActivity.this.convertToPdf(inputPath, outputPath);
// Notify the UI
runOnUiThread(new Runnable() {
public void run()
{
if (result) Toast.makeText(JpgToPdfActivity.this, "The JPG was successfully converted to PDF.", Toast.LENGTH_SHORT).show();
else Toast.makeText(JpgToPdfActivity.this, "An error occured while converting the JPG to PDF.", Toast.LENGTH_SHORT).show();
}
});
}
};
t.start();
}
});
}
public static void convertToPdf(String jpgFilePath, String outputPdfPath)
{
try
{
// Check if Jpg file exists or not
File inputFile = new File(jpgFilePath);
if (!inputFile.exists()) throw new Exception("File '" + jpgFilePath + "' doesn't exist.");
// Create output file if needed
File outputFile = new File(outputPdfPath);
if (!outputFile.exists()) outputFile.createNewFile();
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(outputFile));
document.open();
Image image = Image.getInstance(jpgFilePath);
document.add(image);
document.close();
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
}

Manitoba
- 8,522
- 11
- 60
- 122
-
@poojagupta if the code worked then please mark the question solved and upvote the voter for his effort – Rohan Kandwal Dec 09 '13 at 16:50
-
@manitoba I am facing one issue it is not converting whole image as pdf but only a part of it.Can you help me for that. – poojagupta Dec 10 '13 at 05:33
-
I think you must resize the picture. Take a look at this page http://stackoverflow.com/questions/10413659/how-to-resize-image-in-android – Manitoba Dec 10 '13 at 09:11