如何将多个图像从android文件夹转换为单个PDF?

问题描述 投票:5回答:3

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

ex:

文件夹名称:

-图像

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

[名为images的文件夹中有5张图像

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

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

java android
3个回答
0
投票

在4.4版本之后尝试,它将起作用。

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

0
投票

将您的问题分解为较小的问题。这是一个相当简单的应用程序。

  1. 从用户那里获取文件夹名称。请参阅本机文件打开对话框以找到文件夹。参见here
  2. 在其文件中搜索图像
  3. 创建图像的pdf。使用诸如apache pdfbox之类的库。

0
投票

Use this iText library

创建文档

            String FILE = "{folder-path}/FirstPdf.pdf";
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(FILE));
            document.open();

在文档中添加图像

    try {
            // get input stream
           String fileName = "OfflineMap/abc.jpg";
           String path = 
           Environment.getExternalStorageDirectory()+"/"+fileName;
           File file = new File(path);
           FileInputStream fileInputStream = new FileInputStream(file);
           InputStream ims = getAssets().open("myImage.png");
           Bitmap bmp = BitmapFactory.decodeStream(ims);
           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
           Image image = Image.getInstance(stream.toByteArray());
           document.add(image);
           document.close();
        }
    catch(IOException ex)
        {
            return;
        }
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.