AesculusMaximus AesculusMaximus - 1 month ago 10x
Android Question

PDFrenderer setting scale to screen

Im using the code below to render a pdf. This is in a try/catch and works well, showing the pdf.

The problem is that the pdf file is too big for the screen. Does anyone know how to scale it down to fit please?

Thank you.

imageView = (ImageView) findViewById(;

int REQ_WIDTH = imageView.getWidth();
int REQ_HEIGHT = imageView.getHeight();

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
int width12 = size.x;
int height12 = size.y;

Bitmap bitmap = Bitmap.createBitmap(width12, height12, Bitmap.Config.ARGB_4444);

File file = new File("/sdcard/Download/sample.pdf");

PdfRenderer renderer = new PdfRenderer(, ParcelFileDescriptor.MODE_READ_ONLY));

if (currentPage < 0) {
currentPage = 0;
} else if (currentPage > renderer.getPageCount()) {
currentPage = renderer.getPageCount();

int pages;

pages = renderer.getPageCount();

Matrix m = imageView.getImageMatrix();

Rect rect = new Rect(0, 0, width12, height12);

renderer.openPage(currentPage).render(bitmap, rect, m, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);



You need to create a Bitmap that matches the aspect ratio of the Page. It's best to match the dimensions of the ImageView as well:

                PdfRenderer renderer = new PdfRenderer(, ParcelFileDescriptor.MODE_READ_ONLY));
                PdfRenderer.Page page = renderer.openPage(0);
                int pageWidth = page.getWidth();
                int pageHeight = page.getHeight();
                float scale = Math.min((float) REQ_WIDTH / pageWidth, (float) REQ_HEIGHT / pageHeight);
                Bitmap bitmap = Bitmap.createBitmap((int) (pageWidth * scale), (int) (pageHeight * scale), Bitmap.Config.ARGB_8888);
                page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);


To avoid ImageView having width and height of 0, one solution is to post a Runnable containing the code: Runnable() {
    public void run() {
        // The above code goes here