如何从android中的图像文件创建多页的pdf文件

Dar*_*ini 6 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());
                }

            }
        });
Run Code Online (Sandbox Code Playgroud)

MKJ*_*ekh 3

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

如何在android中用图像生成Pdf文件?

如何使用 Java 和 itext 从 Graphics 对象创建多页 PDF

iText 示例