PDFRenderer APIs To Display PDF Documents

Have you ever wondered why you have to open a PDF file in Android on Chrome or using a 3rd-party viewer? Well, it’s because until now Android hasn’t had a native PDF rendering tool. As of Android “L,” it does. If you’re on Android 4.4 or below, try opening a PDF in the Drive app – you’ll be sent to whatever your native PDF viewing tool happens to be.

Android finally gets native PDFRendering via the new PDFRenderer APIs introduced in android 5.0, lollipop version. In this example we will see how to use PdfRenderer to display PDF documents on the screen.

We can now render PDF document pages into bitmap images for printing by using the new PdfRenderer class. You must specify a ParcelFileDescriptor that is seekable (that is, the content can be randomly accessed) on which the system writes the the printable content. Your app can obtain a page for rendering with openPage(), then call [render()] to turn the opened PdfRenderer.Page into a bitmap.

The api class: PdfRenderer
This class enables rendering a PDF document. This class is not thread safe.
If you want to render a PDF, you create a renderer and for every page you want to render, you open the page, render it, and close the page. After you are done with rendering, you close the renderer. After the renderer is closed it should not be used anymore. Note that the pages are rendered one by one, i.e. you can have only a single page opened at any given time.

This sample loads the PDF from assets. Contents of assets are compressed by default. It uses the Gradle build system. To build this project, use the “gradlew build” command or use “Import Project” in Android Studio.

Steps:

1.) Create a new project in you Android studio.

2.) Write following into MainActivity.java:

 

package com.example.android.pdfrendererbasic;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

public static final String FRAGMENT_PDF_RENDERER_BASIC = "pdf_renderer_basic";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_real);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PdfRendererBasicFragment(),
FRAGMENT_PDF_RENDERER_BASIC)
.commit();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_info:
new AlertDialog.Builder(this)
.setMessage(R.string.intro_message)
.setPositiveButton(android.R.string.ok, null)
.show();
return true;
}
return super.onOptionsItemSelected(item);
}
}

 

 

3.) You can also download the code from github from google

OutPut:

pdfrenderer1
Check below posts I have published previously related to PDF

1.) Getting a PDF from Server

2.) PDFReader

and

3.) List of PDF files on your device

Also you can Check the link below to see some more new features of Android 5.0.

Leave a Comment: