Android render pdf to bitmap with itext library

554 views Asked by At

I am trying to convert a pdf stored in my assets folder into bitmap using PdfViewer.jar.

This is my code:

public static Bitmap renderToBitmap(InputStream inStream) {
    Bitmap bitmap = null;
    try {
        byte[] bArray = IOUtils.toByteArray(inStream);
        ByteBuffer buf = ByteBuffer.wrap(bArray);
        PDFPage mPdfPage = new PDFFile(buf).getPage(0, true);
        float width = mPdfPage.getWidth();
        float height = mPdfPage.getHeight();
        bitmap = mPdfPage.getImage((int) (width), (int) (height), null);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            inStream.close();
        } catch (IOException e) {
            // do nothing because the stream has already been closed
        }
    }
    return bitmap;
}

This code is doing what I need. But the resulting bitmap quality is very poor. How can I increase the quality of bitmap created ?

0

There are 0 answers