how to save image to pdf in android


//make function inside the activity

public void convertPDF(byte[] path)
{
    //String FILE = "mnt/sdcard/FirstPdf.pdf";    Document document=new Document();

    File direct = new File(Environment.getExternalStorageDirectory() + "/collage app pdf");

    if (!direct.exists()) {
        File wallpaperDirectory = new File("/sdcard/collage app pdf/");
        wallpaperDirectory.mkdirs();
    }
    File f = null;
    f = new File(direct, UUID.randomUUID().toString() + ".pdf");
    if (f.exists()) {
        f.delete();
    }





    try {
        PdfWriter.getInstance(document, new FileOutputStream(f));
        document.open();

        try {

            Image image=Image.getInstance(path);
            document.add(new Paragraph("Collage Pic"));
            document.add(image);
            document.close();
            Toast.makeText(getContext(), "pdf save successfully", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

//code on button click

if (item.getItemId() == R.id.menu_save_collage)
{

    BitmapDrawable btmpDr = (BitmapDrawable) mCollageImageView.getDrawable();
    Bitmap bmp = btmpDr.getBitmap();


    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG,100,stream);
    byte[] byteArray=stream.toByteArray();
    convertPDF(byteArray);

    }


//add this in the gradle

compile project(':itextpdf-5.3.2')


Comments

Post a Comment

Popular posts from this blog

how to fetch blob image gallery in php html sql

How to create album view with fancybox slider view in html php complete code