【问题标题】:Issue with Adding Image while Creating Dynamic Pdf in android在android中创建动态Pdf时添加图像的问题
【发布时间】:2013-06-17 06:38:41
【问题描述】:

我使用以下代码动态创建了 pdf,还使用了 iText 库:

try {

                String str_path = Environment.getExternalStorageDirectory()
                        .toString();

                File file;
                file = new File(str_path, getString(R.string.app_name)
                        + ".pdf");
                FileOutputStream fos = new FileOutputStream(file);

                Document document = new Document();
                PdfWriter.getInstance(document, fos);

                document.open();
                document.add(new Paragraph("Hello World"));
                document.add(new Paragraph(new Date().toString()));

                document.close();
                fos.close();

            } catch (Exception e) {

                e.printStackTrace();
            }

比我检索 976 字节的 pdf 文件。 当我使用以下代码添加用于将图像添加到此 pdf 文件的代码时:

    try {

                String str_path = Environment.getExternalStorageDirectory()
                        .toString();

                File file;
                file = new File(str_path, getString(R.string.app_name)
                        + ".pdf");
                FileOutputStream fos = new FileOutputStream(file);

                Document document = new Document();
                PdfWriter.getInstance(document, fos);

                document.open();
                document.add(new Paragraph("Hello World, iText"));
                document.add(new Paragraph(new Date().toString()));


                Image image = Image.getInstance("ic_launcher.png");
                document.add(image);

                document.close();
                fos.close();

            } catch (Exception e) {

                e.printStackTrace();
            }

比我检索 0 字节的 pdf 文件。

我不知道是什么问题,如果您对此有任何想法,请与我分享。谢谢。

【问题讨论】:

    标签: android pdf-generation


    【解决方案1】:

    它会解决你的问题

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] bitMapData = stream.toByteArray();
            Image image = Image.getInstance(bitMapData);
    

    【讨论】:

      【解决方案2】:

      最后我使用以下代码将图像添加到 pdf:

      试试{

                      String str_path = Environment.getExternalStorageDirectory()
                              .toString();
      
                      File file;
                      file = new File(str_path, getString(R.string.app_name)
                              + ".pdf");
                      FileOutputStream fos = new FileOutputStream(file);
      
                      Document document = new Document();
      
                      PdfWriter.getInstance(document, fos);
      
                      InputStream ims = getAssets().open("ic_launcher.png");
                      Bitmap bmp = BitmapFactory.decodeStream(ims);
                      ByteArrayOutputStream stream = new ByteArrayOutputStream();
                      bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                      Image image = Image.getInstance(stream.toByteArray());
      
                      document.open();
                      document.add(new Paragraph("Hello World"));
                      document.add(new Paragraph(new Date().toString()));
                      document.add(image);
      
                      document.close();
                      fos.close();
      
                  } catch (Exception e) {
      
                      e.printStackTrace();
                  }
      

      使用此代码,希望对您有所帮助。

      【讨论】:

        【解决方案3】:

        这里出现异常,因此您检索到 0 字节的 pdf 文件

        【讨论】:

          猜你喜欢
          • 2021-11-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多