Getting Image from drawable and adding to PDF using iText

不想你离开。 提交于 2019-11-29 06:33:44
Festus Tamakloe

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

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

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

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;
    }
rohit
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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!