如何图像文件转换为Android的一个PDF文件 [英] How to convert image file to a pdf file in Android

查看:234
本文介绍了如何图像文件转换为Android的一个PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想图像文件(JPEG)转换到PDF文件在我的Adroid应用。我用的 itextpdf 罐子和 droidtext 罐子。对我来说无论是工作。下面是code,而使用的 itextpdf

I am trying to convert image file (jpeg) in to pdf file in my Adroid application. I have used itextpdf jar and droidtext jar. Neither work for me. Below is the code while using 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异常因为code线 document.close()的一个错误;

This gives me an error with a NullPointerException because of the code line document.close();

当我评论该行并运行程序,它给了我下面的错误。

When I comment that line and run the program, it gives me the following error.

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 的到构建路径也。我用模拟器试过这个问题,以及与真正的设备。

I have added the itextpdf-5.1.3.jar to the build path also. I tried this with a emulator as well as with a real device.

想不通我做错了。请帮助...

Can't figure out what I have done wrong. Please help...

推荐答案

找到一种方法把这个问题解决。我必须做的两个变化在我的code。

Found a way to tackle with this problem. I have to do two changes in my code.


  1. 实施在我的活动 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);
}     

不过我想不出有日志和工作code给出错误的关系: - /

Still I cannot think the relationship with the Error given in the log and the working code :-/

这篇关于如何图像文件转换为Android的一个PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆