Getting Image from drawable and adding to PDF using iText

 ̄綄美尐妖づ 提交于 2020-01-10 02:42:05

问题


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


回答1:


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



回答2:


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



回答3:


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



回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/15742125/getting-image-from-drawable-and-adding-to-pdf-using-itext

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