Display PDF file inside my android application

I cannot figure out how to show a PDF file inside an Android application. So far I've found out that it is possible to launch an Intent and open the PDF using the Android default app. But I want to view PDF file directly inside my application without exiting. I have an header and a footer in my layout - I'd like to open the PDF in between. I have also found a PdfReader.jar file from github.com, but it opens the PDF in a new activity.

120k 32 32 gold badges 168 168 silver badges 192 192 bronze badges asked Mar 12, 2012 at 11:15 Adil Bhatty Adil Bhatty 17.3k 34 34 gold badges 82 82 silver badges 120 120 bronze badges are you try with googledocs viewer ? Commented Mar 12, 2012 at 11:17 I am using google docs viwer, but it lags alot and is very slow :( Commented Mar 12, 2012 at 11:39

8 Answers 8

Add this dependency in your gradle file:

compile 'com.github.barteksc:android-pdf-viewer:2.0.3' 

activity_main.xml

MainActivity.java

package pdfviewer.pdfviewer; import android.app.Activity; import android.os.Bundle; import android.util.Log; import com.github.barteksc.pdfviewer.PDFView; import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener; import com.github.barteksc.pdfviewer.listener.OnPageChangeListener; import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle; import com.shockwave.pdfium.PdfDocument; import java.util.List; public class MainActivity extends Activity implements OnPageChangeListener,OnLoadCompleteListener < private static final String TAG = MainActivity.class.getSimpleName(); public static final String SAMPLE_FILE = "android_tutorial.pdf"; PDFView pdfView; Integer pageNumber = 0; String pdfFileName; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pdfView= (PDFView)findViewById(R.id.pdfView); displayFromAsset(SAMPLE_FILE); >private void displayFromAsset(String assetFileName) < pdfFileName = assetFileName; pdfView.fromAsset(SAMPLE_FILE) .defaultPage(pageNumber) .enableSwipe(true) .swipeHorizontal(false) .onPageChange(this) .enableAnnotationRendering(true) .onLoad(this) .scrollHandle(new DefaultScrollHandle(this)) .load(); >@Override public void onPageChanged(int page, int pageCount) < pageNumber = page; setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount)); >@Override public void loadComplete(int nbPages) < PdfDocument.Meta meta = pdfView.getDocumentMeta(); printBookmarksTree(pdfView.getTableOfContents(), "-"); >public void printBookmarksTree(List tree, String sep) < for (PdfDocument.Bookmark b : tree) < Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx())); if (b.hasChildren()) < printBookmarksTree(b.getChildren(), sep + "-"); >> > >