Display PDF using PdfRenderer


The class android.graphics.pdf.PdfRenderer enables rendering a PDF document. This example show how to:


MainActivity.java
package com.blogspot.android_er.androidpdf;

import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

String targetPdf = "/sdcard/MagPi54.pdf";

ImageView pdfView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pdfView = (ImageView)findViewById(R.id.pdfview);

try {
openPDF();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this,
"Something Wrong: " + e.toString(),
Toast.LENGTH_LONG).show();
}
}

private void openPDF() throws IOException {
File file = new File(targetPdf);

ParcelFileDescriptor fileDescriptor = null;
fileDescriptor = ParcelFileDescriptor.open(
file, ParcelFileDescriptor.MODE_READ_ONLY);

//min. API Level 21
PdfRenderer pdfRenderer = null;
pdfRenderer = new PdfRenderer(fileDescriptor);

final int pageCount = pdfRenderer.getPageCount();
Toast.makeText(this,
"pageCount = " + pageCount,
Toast.LENGTH_LONG).show();

//Display page 0
PdfRenderer.Page rendererPage = pdfRenderer.openPage(0);
int rendererPageWidth = rendererPage.getWidth();
int rendererPageHeight = rendererPage.getHeight();
Bitmap bitmap = Bitmap.createBitmap(
rendererPageWidth,
rendererPageHeight,
Bitmap.Config.ARGB_8888);
rendererPage.render(bitmap, null, null,
PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);

pdfView.setImageBitmap(bitmap);
rendererPage.close();

pdfRenderer.close();
fileDescriptor.close();
}

}


layout/activity_main.xml, add a ImageView to display the PDF:

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidpdf.MainActivity">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"/>

android:id="@+id/pdfview"
android:layout_width="match_parent"
android:layout_height="match_parent" />




uses-permission of "android.permission.READ_EXTERNAL_STORAGE" is needed in AndroidManifest.xml to read file from sdcard.

package="com.blogspot.android_er.androidpdf">

android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">












Next:
Display PDF in assets folder (inside APK)

Related:
Create PDF using PdfDocument

Popular posts from this blog

OnePlus Releases OxygenOS 4.5 OTA For OnePlus 3 and OnePlus 3T

Black Friday saw the lowest price yet for the Roomba j7 from iRobot.

Report: Incredibly Accurate GPS Chips are Coming to Smartphones Next Year