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.}The renderPageToBitmap API in the example above is synchronous and blocks its calling thread, so call it on a background thread or use renderPageToBitmapAsync instead. renderPageToBitmapAsync is a suspend function, so call it from a coroutine — for example, inside lifecycleScope.launch { … }.