【问题标题】:Getting Image from drawable and adding to PDF using iText从drawable获取图像并使用iText添加到PDF
【发布时间】:2013-03-22 10:47:25
【问题描述】:

我想使用 iText 将图像添加到 android PDF。我想在不先将图像保存到 SDCard 的情况下实现这一点。我将图像放入 res/drawable 文件夹,但证明图像路径不起作用并引发 FileNotFound 异常。我的路径是这样的:

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

现在请建议我一个解决方案,我将如何将正确的文件路径添加到 getInstance(...) 方法。谢谢

【问题讨论】:

    标签: android pdf itext drawable


    【解决方案1】:

    当然它不会那样工作。

    将您的图像移动到资产文件夹以使用 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;
            }
    

    【讨论】:

    • 我无法将位图添加到文档添加方法,不支持位图类型:(
    • 资源应该优先于资产,
    • 我可以使用静态高度和宽度来显示将在 pdf 上显示的图像吗?
    • 谢谢,它解决了我的问题,现在我的代码可以正常工作了 :)
    • 工作得很好!任何人都知道如何从 PDF 文件中删除这个添加的图像???
    【解决方案2】:

    我为您的问题找到了解决方案。如果您想从可绘制文件夹中获取图像并使用 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();
    }
    

    【讨论】:

    • 工作得很好!太棒了。
    • 像魅力一样工作!!
    【解决方案3】:

    这是使用 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;
        }
    }
    

    【讨论】:

      【解决方案4】:

      这是我的代码,将图像设置在特定位置 将您的图像移动到资产文件夹以通过 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;
          }
      

      【讨论】:

        【解决方案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);
        }
        

        【讨论】:

          猜你喜欢
          • 2017-12-19
          • 2010-12-22
          • 2014-08-18
          • 2019-03-26
          • 1970-01-01
          • 1970-01-01
          • 2012-10-07
          • 1970-01-01
          • 2018-10-01
          相关资源
          最近更新 更多