Wednesday, February 18, 2015

Android PDFView

Android PDFView is a library which provides a fast PDFView component for Android, with animations, gestures, and zoom.

the reference is taken from PDF View.

We just need to use com.joanzapata.pdfview.PDFView at xml layout as follows

<com.joanzapata.pdfview.PDFView
        android:id="@+id/pdfview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


and than can be used in main activity as follows

pdfView.fromAsset(pdfName)
    .pages(0, 2, 1, 3, 3, 3)
    .defaultPage(1)
    .showMinimap(false)
    .enableSwipe(true)
    .onDraw(onDrawListener)
    .onLoad(onLoadCompleteListener)
    .onPageChange(onPageChangeListener)
    .load();

for a reference here is a full code of

activity_main.xml is as follows


<FrameLayout xmlns: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" >

    <com.joanzapata.pdfview.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

while PDFView can be used in PDFViewActivity as follows

import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnPageChangeListener;

import android.app.Activity;
import android.os.Bundle;

public class PDFViewActivity extends Activity implements OnPageChangeListener {

public static final String SAMPLE_FILE = "sample.pdf";

public static final String ABOUT_FILE = "about.pdf";

PDFView pdfView;

String pdfName = ABOUT_FILE;

Integer pageNumber = 1;

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

pdfView = (PDFView) findViewById(R.id.pdfView);
display(pdfName, false);
}

private void display(String assetFileName, boolean jumpToFirstPage) {
if (jumpToFirstPage)
pageNumber = 1;
setTitle(pdfName = assetFileName);

pdfView.fromAsset(assetFileName).defaultPage(pageNumber)
.onPageChange(this).load();
}

@Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
    setTitle(pdfName = pageNumber+"");
    }
}


make sure that you have sample pdf files in Asset folder of yours project.

the library code can be downloaded from https://github.com/JoanZapata/android-pdfview  and than PDFViewActivity  can be used simply as in above given  example.

Download code from Demo PDF Activity

21 comments:

  1. thankssssssssssss its wrkinggggggggggggg

    ReplyDelete
  2. Hi jit
    Canu like to share how to display PDF from server..although I did but throwing
    Me error of PDF document size is 0
    Kindly assist
    Thanks

    ReplyDelete
    Replies
    1. Please search an example at github and https://android-arsenal.com/ as personally i did not got chance to work on that part :)

      Delete
  3. Wish I can express as you do. What a blog? Can you give the names of websites who are paying for bloggers? I am sure you must be making tons of money. I want to do the same, but in own sweet time. Meanwhile I have worked on my interests and posted some micro-jobs on selling my logo design,eye catching logo and www.envato.com. Mostly I have my writing skills on offer. Many have responded. I am happy as anyone can be.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi Jitesh ,


    Thanks your code working good i want to swipe vertically for page navigation inspect of horizontal swiping .

    Kindly assist
    Thanks

    ReplyDelete
  6. Hi Jitesh ,


    Thanks your code working good i want to swipe vertically for page navigation inspect of horizontal swiping .

    Kindly assist
    Thanks

    ReplyDelete
  7. Hi Jitesh,

    I try to give another pdf in asset folder instead of about.pdf but its not working or on emulator its remain as it is after Run as its not change.

    please give me solution.

    Kindly assist
    Thanks

    ReplyDelete
    Replies
    1. hi i am not sure why the another pdf is not working while first one is running , so please can you give me some logs that i can look in to them.

      Delete
  8. Hi,
    Please guide me how can I show a pdf file from SD card (not assets).

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. pdfView.fromFile(new File("pathtofile"))...

      Delete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Dear Jitesh,

    Can you send me your demo pdf activity code as I am not able to download it from the site? Can send to cliffordng888@live.com

    Thanks a lot

    ReplyDelete
  11. Dear Jitesh,

    I am new to Android development and is doing an apps to view pdf within the apps. Your help is much appreciated if you could send me your demo code. Thanks

    ReplyDelete
  12. How to programatically change the page using this library ?

    ReplyDelete
  13. How did you include the library as a folder in build path ?

    ReplyDelete
  14. hi,its working but images are not loading :(

    ReplyDelete