【问题标题】:How to convert image file to a pdf file in Android如何在 Android 中将图像文件转换为 pdf 文件
【发布时间】:2012-09-01 03:44:57
【问题描述】:

我正在尝试在我的 Adroid 应用程序中将图像文件 (jpeg) 转换为 pdf 文件。我用过 itextpdf jar 和 droidtext jar。都不适合我。下面是使用 itextpdf 时的代码。

Document document = new Document();

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

File newPdfFile = new File(directoryPath, "textview8.pdf");

FileOutputStream fileOutputStream = null;

try {
    fileOutputStream = new FileOutputStream(newPdfFile);
} catch (FileNotFoundException fnfe) {
    Log.w(TAG, "# Exception caz of fileOutputStream : " + fnfe);
}

BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

try {
    PdfWriter.getInstance(document, bufferedOutputStream);
} catch (DocumentException de) {
    Log.w(TAG, "# Exception caz of PdfWriter.getInstance : " + de);
}

document.open(); 

Image image = null;

try {
    image = Image.getInstance(directoryPath + File.separator + "textview1.JPEG");
} catch (BadElementException bee) {
    Log.w(TAG, "# First exception caz of image : " + bee);
} catch (MalformedURLException mue) {
    Log.w(TAG, "# Second exception caz of image : " + mue);
} catch (IOException ioe) {
    Log.w(TAG, "# Third exception caz of image : " + ioe);
}

try {
    document.add(image);
} catch (DocumentException de) {
    Log.w(TAG, "# Exception caz of document.add : " + de);
}

try {
    bufferedOutputStream.flush();
    bufferedOutputStream.close();

    fileOutputStream.flush();
    fileOutputStream.close();

} catch (IOException ioe) {
    Log.w(TAG, "# Exception caz of bufferedOutputStream.flush : " + ioe);
}

document.close();

这给了我一个NullPointerException 的错误,因为代码行document.close();

当我注释该行并运行程序时,它给了我以下错误。

Could not find class 'com.itextpdf.awt.PdfPrinterGraphics2D', referenced from method com.itextpdf.text.pdf.PdfContentByte.createPrinterGraphicsShapes

但是他们告诉找不到的类已经在jar文件中,这意味着com.itextpdf.awt.PdfPrinterGraphics2D存在于项目中。

我也将 itextpdf-5.1.3.jar 添加到构建路径中。我在模拟器和真实设备上都试过了。

无法弄清楚我做错了什么。请帮忙...

【问题讨论】:

    标签: android image pdf itextpdf


    【解决方案1】:

    只要这样做就可以正常工作

    Document document=new Document();
    String dirpath=android.os.Environment.getExternalStorageDirectory().toString();
    PdfWriter.getInstance(document,new FileOutputStream(dirpath+"/imagedemo.pdf"));
    document.open();
    Image im=Image.getInstance(dirpath+"/"+"logo.png");  // Replace logo.png with your image name with extension 
    document.add(im);
    document.close();
    

    【讨论】:

    • 我只用过 itextpdf-5.1.1.jar
    • 实际上你的答案是正确的,直到你不打算关闭FileOutputStream。关闭已打开的流是一种很好的做法。所以我能够找到解决方案。希望你不介意我把它作为一个答案:-)
    【解决方案2】:

    我看到的所有例子都是直接使用FileOutputStream,你试过不带buffer吗?

    PdfWriter.getInstance(document, fileOutputStream);
    

    【讨论】:

      【解决方案3】:

      找到了解决这个问题的方法。我必须对我的代码进行两次更改。

      1. 在我的Activity 中实现DocListener 接口
      2. 关闭文档后关闭流

      这里是第二点的变化

      try {
          bufferedOutputStream.flush();
      
          fileOutputStream.flush();
      
          } catch (IOException ioe) {
              Log.w(TAG, "# Exception caz of flush : " + ioe);
          }
      
          document.close();
      
      try {
          bufferedOutputStream.close();
      
          fileOutputStream.close();
      
          } catch (IOException ioe) {
              Log.w(TAG, "# Exception caz of close : " + ioe);
      }     
      

      我仍然无法想到与日志中给出的错误和工作代码的关系:-/

      【讨论】:

        猜你喜欢
        • 2014-07-17
        • 2018-01-25
        • 2013-08-13
        • 1970-01-01
        • 2015-03-10
        • 1970-01-01
        • 2012-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多