【问题标题】:how to create pdf file with multiple pages from image file in android如何从android中的图像文件创建具有多个页面的pdf文件
【发布时间】:2012-11-16 05:01:09
【问题描述】:

如何从 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());
                }

            }
        });

【问题讨论】:

    标签: android pdf-generation


    【解决方案1】:

    如果您想创建一个包含多个图像的 pdf 文件,您可以使用 Android 中的PdfDocument。这是一个演示:

    private void createPDFWithMultipleImage(){
        File file = getOutputFile();
        if (file != null){
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                PdfDocument pdfDocument = new PdfDocument();
    
                for (int i = 0; i < images.size(); i++){
                    Bitmap bitmap = BitmapFactory.decodeFile(images.get(i).getPath());
                    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), (i + 1)).create();
                    PdfDocument.Page page = pdfDocument.startPage(pageInfo);
                    Canvas canvas = page.getCanvas();
                    Paint paint = new Paint();
                    paint.setColor(Color.BLUE);
                    canvas.drawPaint(paint);
                    canvas.drawBitmap(bitmap, 0f, 0f, null);
                    pdfDocument.finishPage(page);
                    bitmap.recycle();
                }
                pdfDocument.writeTo(fileOutputStream);
                pdfDocument.close();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    private File getOutputFile(){
        File root = new File(this.getExternalFilesDir(null),"My PDF Folder");
    
        boolean isFolderCreated = true;
    
        if (!root.exists()){
            isFolderCreated = root.mkdir();
        }
    
        if (isFolderCreated) {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
            String imageFileName = "PDF_" + timeStamp;
    
            return new File(root, imageFileName + ".pdf");
        }
        else {
            Toast.makeText(this, "Folder is not created", Toast.LENGTH_SHORT).show();
            return null;
        }
    }
    

    这里的 images 是带有路径的图像的 ArrayList。

    【讨论】:

      【解决方案2】:

      如果您的线性布局高度非常大,请尝试此代码

      https://demonuts.com/android-generate-pdf-view/

      点击此链接,只需更改线路

      PdfDocument document = new PdfDocument();
      PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(convertWidth, 10000, 1).create();
      PdfDocument.Page page = document.startPage(pageInfo);
      Canvas canvas = page.getCanvas();
      Paint paint = new Paint();
      paint.setColor(Color.BLUE);
      canvas.drawPaint(paint);
      Bitmap bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, 10000, true);
      canvas.drawBitmap(bitmap, 0, 0 , null);
      document.finishPage(page);`
      

      【讨论】:

        【解决方案3】:

        你可以这样设置图片...

        Bitmap bt=Bitmap.createScaledBitmap(btm, 200, 200, false);
        bt.compress(Bitmap.CompressFormat.PNG,100, bos);
        

        【讨论】:

          【解决方案4】:

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

          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

          【讨论】:

          • 我使用 itext.jar 文件来创建 pdf。但在创建多页 PDF 文件时出现了一些问题。
          猜你喜欢
          • 2015-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-07
          • 2017-02-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多