android - 如何从android中的图像文件创建包含多个页面的pdf文件

标签 android pdf-generation

如何在 Android 中从图像文件创建包含多页的 PDF 文件?我从图像创建了一个 PDF 文件。该 PDF 文件只有一页。那是该图像的一半。在右侧搜索部分被剪切成 PDF 文件。 我正在使用 itext-5.3.4.jar 创建 PDF。

    wbviewnews.loadUrl("http://developer.android.com/about/index.html");
   // button for create wbpage to image than image to PDF file
            Button  btnclick =(Button)findViewById(R.id.btnclick);
            btnclick.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Picture p = wbviewnews.capturePicture();
                bitmap=null;


                PictureDrawable pictureDrawable = new PictureDrawable(p);

                bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
                //Bitmap bitmap = Bitmap.createBitmap(200,200, Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                canvas.drawPicture(pictureDrawable.getPicture());

                ImageView imgdata=(ImageView)findViewById(R.id.imgdata);
                imgdata.setImageBitmap(bitmap); 

                String filename = "pippo.png";
                File sd = Environment.getExternalStorageDirectory();

                File dest = new File(sd, filename);
                String pdffilename = "pippo.pdf";
                File pdffilepath = new File(sd, pdffilename);


                try {
                    FileOutputStream out = new FileOutputStream(dest);

                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                    out.flush();
                    out.close();

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("Exception", e.toString());
                }

                Document document=new Document();


                try {
                    Log.e("pdffilepath", pdffilepath.toString());
                    PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(pdffilepath));
                    document.open();

                    //  URL url = new URL (Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+filename);
                    //  Log.e("url", url.toString());
                    Image image = Image.getInstance(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+filename) ;

                    document.add(image);               
                    document.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("FileNotFoundException", e.toString());
                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("DocumentException", e.toString());
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("MalformedURLException", e.toString());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("IOException", e.toString());
                }

            }
        });

最佳答案

我猜你在 StackOverflow 上的搜索较少,因为我发现这些答案已经有了解决方案,是的,它包含在不同的答案中,查看 Q/A 我猜他们可以解决你的问题,如果不能,请继续尝试 :)

how to Generate Pdf File with Image in android?

How to create a PDF with multiple pages from a Graphics object with Java and itext

iText Example

关于android - 如何从android中的图像文件创建包含多个页面的pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13603463/

相关文章:

java - java中的Stripe InvalidRequestException

android - Dagger 2 无法解析符号 'DaggerAppComponent'

java - 使警报对话框的相对布局适合居中

android - Android : Android App is not responding, close it?

emacs - Emacs 打印缓冲区的自动换行到 PDF

需要 PHP HTML5 到 PDF 转换器

java - 如何使用cq5中的页面内容生成CQ5.6.1中的PDF

java - Android 多屏灵活性

在 Windows 和 Linux (Debian) 上通过 Jasper Reports 导出时 PDF 看起来不同

pdf - 最小的有效 PDF 是多少?