Showing posts with label pdfrenderer. Show all posts
Showing posts with label pdfrenderer. Show all posts
Thursday, April 13, 2017
Display PDF using PdfRenderer
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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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">
<TextView
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"/>
<ImageView
android_id="@+id/pdfview"
android_layout_width="match_parent"
android_layout_height="match_parent" />
</LinearLayout>
uses-permission of "android.permission.READ_EXTERNAL_STORAGE" is needed in AndroidManifest.xml to read file from sdcard.
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.blogspot.android_er.androidpdf">
<application
android_allowBackup="true"
android_icon="@mipmap/ic_launcher"
android_label="@string/app_name"
android_supportsRtl="true"
android_theme="@style/AppTheme">
<activity android_name=".MainActivity">
<intent-filter>
<action android_name="android.intent.action.MAIN" />
<category android_name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android_name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
Next:
- Display PDF in assets folder (inside APK)
Related:
- Create PDF using PdfDocument
Labels:
display,
pdf,
pdfrenderer,
using
Subscribe to:
Posts (Atom)