How to convert image file to a pdf file in Android

2019-05-28 02:41发布

问题:

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...

回答1:

Just simply do this way it works fine

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();


回答2:

All the examples I've seen directly use the FileOutputStream, did you try without buffer?

PdfWriter.getInstance(document, fileOutputStream);


回答3:

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

  1. Implement DocListener interface in my Activity
  2. Close streams after closing the document

Here are the changes of the second point

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);
}     

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