我正在尝试在 Android 应用程序中将图像文件(jpg)转换为 pdf 文件。我用过itextpdf罐子和机器人文本罐。都不适合我。下面是使用时的代码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也到构建路径。我用模拟器和真实设备尝试了这一点。
无法弄清楚我做错了什么。请帮忙...