Я пытаюсь преобразовать файл изображения (jpeg) в файл pdf в своем приложении для Android. Я использовал 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 в путь сборки. Я пробовал это как с эмулятором, так и с реальным устройством.
Не могу понять, что я сделал не так. Пожалуйста помоги...