-2

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

poojagupta
  • 982
  • 2
  • 12
  • 26

1 Answers1

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