如何将多个图像从android中的文件夹转换为单个PDF? [英] how convert multiple images to single PDF from folder in android?

查看:53
本文介绍了如何将多个图像从android中的文件夹转换为单个PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让android应用程序将多个图像从单个文件夹合并到单个pdf文件中.

I want to make android app to merge multiple image from single folder into single pdf file.

ex:

文件夹名称:

-图片

    - 1.jpg
    - 2.jpg
    - 3.jpg
    - 4.jpg
    - 5.jpg

在名为 images

我该如何为这些图像制作pdf文件?

如果有人有可能的解决方案,请评论答案:)

推荐答案

在4.4版之后可以尝试使用它.

Try this after 4.4 version it will work.

  private void createPDF() {
    final File file = new File(uploadFolder, "AnswerSheet_" + queId + ".pdf");

    final ProgressDialog dialog = ProgressDialog.show(this, "", "Generating PDF...");
    dialog.show();
    new Thread(() -> {
        Bitmap bitmap;
        PdfDocument document = new PdfDocument();
           //  int height = 842;
             //int width = 595;
        int height = 1010;
        int width = 714;
        int reqH, reqW;
        reqW = width;

        for (int i = 0; i < array.size(); i++) {
               //  bitmap = BitmapFactory.decodeFile(array.get(i));
            bitmap = Utility.getCompressedBitmap(array.get(i), height, width);


            reqH = width * bitmap.getHeight() / bitmap.getWidth();
            Log.e("reqH", "=" + reqH);
            if (reqH < height) {
                  //  bitmap = Bitmap.createScaledBitmap(bitmap, reqW, reqH, true);
            } else {
                reqH = height;
                reqW = height * bitmap.getWidth() / bitmap.getHeight();
                Log.e("reqW", "=" + reqW);
                  //   bitmap = Bitmap.createScaledBitmap(bitmap, reqW, reqH, true);
            }
            // Compress image by decreasing quality
                // ByteArrayOutputStream out = new ByteArrayOutputStream();
               //  bitmap.compress(Bitmap.CompressFormat.WEBP, 50, out);
             //    bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
            //bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
              //Create an A4 sized page 595 x 842 in Postscript points.
          //PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, 1).create();
            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(reqW, reqH, 1).create();
            PdfDocument.Page page = document.startPage(pageInfo);
            Canvas canvas = page.getCanvas();

            Log.e("PDF", "pdf = " + bitmap.getWidth() + "x" + bitmap.getHeight());
            canvas.drawBitmap(bitmap, 0, 0, null);

            document.finishPage(page);
        }

        FileOutputStream fos;
        try {
            fos = new FileOutputStream(file);
            document.writeTo(fos);
            document.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        runOnUiThread(() -> {
            dismissDialog(dialog);

        });
    }).start();
}

这篇关于如何将多个图像从android中的文件夹转换为单个PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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