3

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

ex :

folder name :

- images

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

there are 5 images are in folder named images

how can i make pdf of that images?

if anyone have possible solution then please comment answer :)

Mahendra Gohil
  • 380
  • 1
  • 3
  • 21
Praful Korat
  • 374
  • 4
  • 22

4 Answers4

8

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();
}
haresh
  • 1,424
  • 2
  • 12
  • 18
6

If you want to create a pdf file with multiple images you can use PdfDocument from Android. Here is a demo:

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

Here images is the ArrayList of the images with path.

Tushar Pramanik
  • 171
  • 2
  • 4
0

Break your problem down into smaller problems. It's a fairly simple application.

  1. Get the folder name from the user. See the native file open dialog to find a folder. See here.
  2. Search its files for images
  3. Create a pdf of the images. Use a library such as apache pdfbox.
geco17
  • 5,152
  • 3
  • 21
  • 38
0

Use this iText library

Create a document

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

Add image in the document

    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;
        }
Chetan Shelake
  • 656
  • 1
  • 6
  • 14