Generating thumbnails in PDF

PDFTron's library provides a page thumbnail dialog which shows thumbnails of all PDF document pages. Fore more information, see page thumbnails dialog fragment.

With PDFViewCtrl, you can get a thumbnail Bitmap image of each page in PDF document.

  1. To add a async listener for receiving a page thumbnail image, call PDFViewCtrl.addThumbAsyncListener(PDFViewCtrl.ThumbAsyncListener). When the page thumbnail is ready, onThumbReceived(int, int[], int, int) will be invoked. The second parameter represents the received thumbnail image buffer.

  2. To create a Bitmap object from the image buffer, call: Bitmap.createBitmap(int[], int, int, Bitmap.Config).

    For example, the following code snippet puts received thumbnail image buffer to an ImageView when a page thumbnail is received.

    pdfViewCtrl.addThumbAsyncListener(new PDFViewCtrl.ThumbAsyncListener() {
        @Override
        public void onThumbReceived(int page, int[] buf, int width, int height) {
            ImageMemoryCache imageMemoryCache = ImageMemoryCache.getInstance();
            Bitmap bitmap = imageMemoryCache.getBitmapFromReusableSet(width, height, Bitmap.Config.ARGB_8888);
            if (bitmap == null) {
              try{
                bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
              } (OutOfMemoryError oom) {
                Utils.manageOOM(pdfViewCtrl);
              }
            }
            bitmap.setPixels(buf, 0, width, 0, 0, width, height);
            imageMemoryCache.addBitmapToReusableSet(bitmap);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setImageBitmap(bitmap);
      }
    });

    where pdfViewCtrl is an instance of PDFViewCtrl, and imageView is an instance of ImageView.

    As the size of ImageView may not be as large as the thumbnail image, it's recommended to resize the bitmap to save memory. Alternatively, you can also use Utils.manageOOM(PDFViewCtrl) to free up memory if an out of memory error occurs.
  1. To request a page thumbnail, call PDFViewCtrl.getThumbAsync(int). Please note that if multiple requests are made, the thumnails will be processed starting with the last request.

    try {
        // getting the thumbnail of page 3
        pdfViewCtrl.getThumbAsync(3);
    } catch (PDFNetException e) {
        e.printStackTrace();
    }

    where pdfViewCtrl is an instance of PDFViewCtrl.

    It is recommended you cancel all thumbnail requests once they are no longer needed. To cancel all thumbnail request, use PDFViewCtrl.cancelAllThumbRequests().