2

I am trying to display pdf document using following code

pdfRenderer = new PdfRenderer(parcelFileDescriptor);
PdfRenderer.Page page = pdfRenderer.openPage(position);
Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),
                Bitmap.Config.ARGB_8888);
        page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
        imageView.setImageBitmap(bitmap);

The problem is that the pdf displayed is very blur. I show comparison with pdf shown by wps office application in following image.

enter image description here

How to improve the bitmap quality and performance as my app seems to work quite slow.

Community
  • 1
  • 1
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122

2 Answers2

2

Try this:

pdfRenderer = new PdfRenderer(parcelFileDescriptor);
PdfRenderer.Page page = pdfRenderer.openPage(position);
Bitmap bitmap = Bitmap.createBitmap(
    getResources().getDisplayMetrics().densityDpi * mCurrentPage.getWidth() / 72,                        
    getResources().getDisplayMetrics().densityDpi * mCurrentPage.getHeight() / 72,
    Bitmap.Config.ARGB_8888
);
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
imageView.setImageBitmap(bitmap);

where:

  1. getResources().getDisplayMetrics().densityDpi is the target DPI resolution
  2. mCurrentPage.getWidth() returns width in Postscript points, where each pt is 1/72 inch.
  3. 72 (DPI) is the default PDF resolution.

Hence, diving #2 by 72 we get inches and multiplying by DPI we get pixels. In other words to match the quality of the printing device of the display you should increase the size of the image rendered as default PDF resolution is 72 DPI. Please also check this post?

Credits: https://stackoverflow.com/a/32327174/10471480

Mangaldeep Pannu
  • 3,738
  • 2
  • 26
  • 45
2

Answer by @Mangaldeep is correct. However, he does not address memory requirements.

Please follow this article https://developer.android.com/topic/performance/graphics/load-bitmap

In your case you can increase the size of bitmap by a factor, say

  x = size.x/page.getWidth(). 

Where size.x is your screen size. Width. Now you can create new bitmap like

 Bitmap b = Bitmap.create(x*page.getWidth(), page.getHeight(),....);

Hope this helps.