Home » Android » android – Getting Image from drawable and adding to PDF using iText

android – Getting Image from drawable and adding to PDF using iText

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to add image to android PDF using iText. I want to achieve this without saving image to SDCard first. I put my image into res/drawable folder but proving the image path doesn’t work and it throws FileNotFound Exception. My path is like this:

String path = “res/drawable/myImage.png”
Image image = Image.getInstance(path);
document.add(image);

Now please suggest me a solution how I will add correct file path to getInstance(…) method. Thanks

How to&Answers:

Of course it’ll not work at that way.

move your image to assets folder to access it with getassets() method

// load image
    try {
            // get input stream
           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);
        }
   catch(IOException ex)
        {
            return;
        }

Answer:

I found a solution for your issue. If you want to get image from your drawable folder and put it into a PDF file using iText use this code:

try {
    document.open();
    Drawable d = getResources().getDrawable(R.drawable.myImage);
    BitmapDrawable bitDw = ((BitmapDrawable) d);
    Bitmap bmp = bitDw.getBitmap();  
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    Image image = Image.getInstance(stream.toByteArray());
    document.add(image);    
    document.close();
} catch (Exception e) {
      e.printStackTrace();
}

Answer:

Here is the code to add image to PDF using iText, if the image is dynamic (i.e), if the image cannot be added to asset folder at compile time,

public void addImage(Document document,ImageView ivPhoto) throws DocumentException {
try {
     BitmapDrawable drawable = (BitmapDrawable) ivPhoto.getDrawable();       
     Bitmap bitmap = drawable.getBitmap();

     ByteArrayOutputStream stream = new ByteArrayOutputStream();    
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);                             
     byte[] imageInByte = stream.toByteArray();
     Image image = Image.getInstance(imageInByte);
     document.add(image);
    }
    catch(IOException ex)
    {
        return;
    }
}

Answer:

Here is my code, To set Image on particular position
move your image to assets folder to get image by getassets() method.
Hope this will help you!

   try {

        InputStream ims = getAssets().open("header1.png");
        Bitmap bmp = BitmapFactory.decodeStream(ims);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
        Image image = Image.getInstance(stream.toByteArray());
        image.setAbsolutePosition(10f,750f);
        image.scaleToFit(850,78);
        document.add(image);
    }
    catch(IOException ex)
    {
        ex.printStackTrace();
        return;
    }

Answer:

try {
    FileInputStream in = new FileInputStream("input file uri");
    PdfReader pdfReader = new PdfReader(in);
    PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("output file uri"));
    PdfContentByte content = pdfStamper.getOverContent(1);
   Image deliverImg = Image.getInstance("image URI");
   deliverImg.setAbsolutePosition(420f, 100f);
   content.addImage(deliverImg);
   pdfStamper.close();
} catch (DocumentException de) {
   Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
   Log.e("PDFCreator", "ioException:" + e);
}