Convert PDFs to images on Android
To render an image from a PDF file in Nutrient Android SDK, you need to do the following:
try { // Create a Uri for the PDF file. val uri = Uri.parse("path/to/file.pdf") // Instantiate a `Document` from the PDF file's URL. val document = PdfDocumentLoader.openDocument(context, uri) val pageIndex = 0 val pageImageSize = document.getPageSize(pageIndex).toRect() // Create the image val bitmap = document.renderPageToBitmap( context, pageIndex pageImageSize.width().toInt() pageImageSize.height().toInt() ) } catch(ex: Exception) { // Handle error. }
try { // Create a Uri for the PDF file. final Uri uri = Uri.parse("path/to/file.pdf"); // Instantiate a `Document` from the PDF file's URL. final PdfDocument document = PdfDocumentLoader.openDocument(context, uri); final int pageIndex = 0; final RectF pageImageSize = document.getPageSize(pageIndex).toRect(); // // Create the image. final Bitmap bitmap = document.renderPageToBitmap( context, pageIndex, (int)pageImageSize.width() (int)pageImageSize.height() ); } catch(final Exception exception) { // Handle error. }
Note that the renderPageToBitmap
API used in the above example is a synchronous API that will block whatever thread it’s called into, so make sure to either call it on a background thread or use the asynchronous method, renderPageToBitmapAsync
.