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();
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
But the class they are telling can't find is already in the jar file, which means com.itextpdf.awt.PdfPrinterGraphics2D is existing in the project.
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...
Just simply do this way it works fine
All the examples I've seen directly use the FileOutputStream, did you try without buffer?
Found a way to tackle with this problem. I have to do two changes in my code.
DocListener
interface in myActivity
Here are the changes of the second point
Still I cannot think the relationship with the Error given in the log and the working code :-/