Like Us on Facebook

How to Create PDF Reader Application in Android

Hi, in this tutorial I am going to discuss how to create PDF Reader in Android Application. PDF Reader is very useful and Important Application for many Android user to read information. Embedding functionality to Create and Display PDF within Android Application can increase engagement of your user significantly. I am going to make simple PDF Reader application in Android, this can be improved By adding more controls.


How to Create  PDF Reader Application in Android theme uandblog

 

Prerequisites:

  • JDK 7.0 or Above
  • Android Studio 2.0
 

Steps to follow:

Step 1: Create a New Android Studio project namePDFReaderDemo.

Step 2: Select minimum API level 21 so  that App will run Android Lolipopand its  upper version

Step 3: Give your layout xml name is activity_main And click finish .

Step 4: Inside  MainActivity write the following code.
 
public class MainActivity extends AppCompatActivity {
 
privateImageViewimg;
privateintcurrentpage = 0;
private Button next, previous;
 
 
    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
 
 
next.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View v) {
currentpage++;
render();
            }
        });
 
 
previous.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View v) {
currentpage--;
render();
            }
        });
 
 
    }
 
private void render() {
 
try {
 
img = (ImageView) findViewById(R.id.image);
 
int width = img.getWidth();
int height = img.getHeight();
 
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
 
            File file = new File("/sdcard/downloads/Example.pdf");
 
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
 
if(currentpage<0){
currentpage =0;
 
}else if(currentpage>renderer.getPageCount()){
 
 
currentpage = renderer.getPageCount() -1;
 
Matrix  matrix = img.getImageMatrix();
 
Rectrect = new Rect(0,0, width , height);
 
renderer.openPage(currentpage).render(bitmap,rect,matrix , PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY );
 
img.setImageMatrix(matrix);
img.setImageBitmap(bitmap);
img.invalidate();
 
            }
 
 
        } catch (Exception e) {
 
e.printStackTrace();
 
        }
 
    }
}
 
 

Step 5: Inside  activity_main.xml  write the following code.
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
 
 
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/image"
android:scaleType="fitCenter"
        />
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
 
<Button
android:id="@+id/previous"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="<<"
 
            />
 
 
<Button
android:id="@+id/next"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=">>"
 
            />
 
 
 
</LinearLayout>
 
 
</LinearLayout>
 
 

Step 6: Add the following  permission  to the manifest.xml
 

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 
 

Now compile and run your Application.

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 1 )

Rohit Bansal:
Hi , i want to fetch text file or pdf file from phone on button click and show in perticular imageview .....like fetch image from gallery and show imageview . can you please help me

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts

    You May Like Also