从drawable获取图像并使用iText添加到PDF

问题描述 投票:10回答:5

我想使用iText将图像添加到android PDF。我想首先实现这一目标,而不是将图像保存到SDCard。我将我的图像放入res / drawable文件夹,但证明图像路径不起作用,它会抛出FileNotFound Exception。我的道路是这样的:

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

现在请建议我如何为getInstance(...)方法添加正确的文件路径。谢谢

android pdf itext drawable
5个回答
28
投票

当然它不会那样工作。

将您的图像移动到assets文件夹以使用getassets()方法访问它

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

10
投票

我为您的问题找到了解决方案。如果您想从可绘制文件夹中获取图像并使用iText将其放入PDF文件,请使用以下代码:

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

1
投票

以下是使用iText将图像添加到PDF的代码,如果图像是动态的(即),如果图像在编译时无法添加到资源文件夹,

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

0
投票

这是我的代码,要在特定位置设置图像,请将图像移动到资源文件夹,以通过getassets()方法获取图像。希望对你有帮助!

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

-1
投票
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);
}
© www.soinside.com 2019 - 2024. All rights reserved.