Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display PDF file inside my android application

Tags:

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.

like image 819
Adil Bhatty Avatar asked Mar 12 '12 11:03

Adil Bhatty


People also ask

How do I add a PDF to my Android screen?

Add PDF Shortcut on SamsungOpen the My Files app on your Samsung Galaxy phone. Go to the folder containing the PDF. Touch and hold the file to select it, then tap on the three-dot icon at the bottom and select “Add to Home screen” from the context menu.

How do I view local PDF files on Android?

From android OS 5.0(lollipop) on-wards you can use PdfRenderer instead of webview/library. You can use this class to show pdf's within the app. If you want to support OS lower than that you can use a library/other approach mentioned in other answer as there is no native support.


2 Answers

You can download the source from here(Display PDF file inside my android application)

Add this dependency in your gradle file:

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

activity_main.xml

<RelativeLayout android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#ffffff"     xmlns:android="http://schemas.android.com/apk/res/android" >      <TextView         android:layout_width="match_parent"         android:layout_height="40dp"         android:background="@color/colorPrimaryDark"         android:text="View PDF"         android:textColor="#ffffff"         android:id="@+id/tv_header"         android:textSize="18dp"         android:gravity="center"></TextView>      <com.github.barteksc.pdfviewer.PDFView         android:id="@+id/pdfView"         android:layout_below="@+id/tv_header"         android:layout_width="match_parent"         android:layout_height="match_parent"/>       </RelativeLayout> 

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<PdfDocument.Bookmark> 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 + "-");             }         }     }  } 
like image 114
Deepshikha Puri Avatar answered Oct 18 '22 18:10

Deepshikha Puri


Maybe you can integrate MuPdf in your application. Here is I've described how to do this: Integrate MuPDF Reader in an app

like image 25
Yury Avatar answered Oct 18 '22 19:10

Yury