How to open .pdf file using PdfViewer in Android

I want to open a .pdf file in my Android application using the PdfViewer library. I downloaded the library file and added it to the lib folder. I also added manifest permissions. Now, after viewing the file, the Loader “Download PDF Page” dialog box appears and the file will not be displayed. Here is my code.

// View PDF File

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("application/pdf"); startActivityForResult(intent, PICK_FILE_REQUEST); 

// onActivityResult

 public void onActivityResult(final int requestCode, int resultCode, Intent data) { try { switch (requestCode) { case PICK_FILE_REQUEST: if (resultCode == RESULT_OK) { try { //To read PDF file Uri selectedFile = data.getData(); String path = selectedFile.getPath().toString(); final Intent intent = new Intent(MainActivity.this, ViewPdf.class); intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path); startActivity(intent); } catch (Exception e) { ShowDialog_Ok("Error", "Cannot Open File"); } } break; } } catch (Exception e) { } } 

ViewPdf.class

 public class ViewPdf extends PdfViewerActivity { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } public int getPreviousPageImageResource() { return R.drawable.left_arrow; } public int getNextPageImageResource() { return R.drawable.right_arrow; } public int getZoomInImageResource() { return R.drawable.zoom_in; } public int getZoomOutImageResource() { return R.drawable.zoom_out; } public int getPdfPasswordLayoutResource() { return R.layout.pdf_file_password; } public int getPdfPageNumberResource() { return R.layout.dialog_pagenumber; } public int getPdfPasswordEditField() { return R.id.etPassword; } public int getPdfPasswordOkButton() { return R.id.btOK; } public int getPdfPasswordExitButton() { return R.id.btExit; } public int getPdfPageNumberEditField() { return R.id.pagenum_edit; } } 
0
android pdf pdfviewer
May 05 '15 at 6:44
source share
1 answer

add PDFViewer as a library to your project, copy your resources to yours as described in HERE. This is working code for me. I am reading pdf from the data folder. Make the customization as your requirement. 1. Download the PDF reader project from the link above. 2. Copy all resources to your project. 3. Expand your activity (in which you want to display pdf) from PdfViewerActivity. 4. Copy all the methods from the pdfreader project into your activity. 5.Run. Happy coding

  AssetManager assetManager = getAssets(); InputStream in = null; OutputStream out = null; File file = new File(getFilesDir(), "ABC.pdf"); try { in = assetManager.open("ABC.pdf"); out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE); copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch (Exception e) { Log.e("tag", e.getMessage()); } Intent intent = new Intent(this, YourNextActivityName.class); intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getFilesDir() + "/ABC.pdf"); startActivity(intent); } private void copyFile(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } } 

Yourpdfview Activity Methods

  public int getPreviousPageImageResource() { return R.drawable.left_arrow; } public int getNextPageImageResource() { return R.drawable.right_arrow; } public int getZoomInImageResource() { return R.drawable.zoom_in; } public int getZoomOutImageResource() { return R.drawable.zoom_out; } public int getPdfPasswordLayoutResource() { return R.layout.pdf_file_password; } public int getPdfPageNumberResource() { return R.layout.dialog_pagenumber; } public int getPdfPasswordEditField() { return R.id.etPassword; } public int getPdfPasswordOkButton() { return R.id.btOK; } public int getPdfPasswordExitButton() { return R.id.btExit; } public int getPdfPageNumberEditField() { return R.id.pagenum_edit; } 
+1
May 11 '15 at 11:21
source share



All Articles