.JPG转换为PDF [英] Converting .jpg to pdf

查看:179
本文介绍了.JPG转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用我已经通过点击摄像机图像并存储在SD卡现在我想将它转换成PDF格式的点击,我不知道该怎么办it.Can任何人帮助我​​。
我想acheive它在button.is的点击就可以

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

推荐答案

下面是一个code,它应该做的工作。我试着注释行的最高限额,但如果你不明白,请告诉我。

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;
    }
}

这篇关于.JPG转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆