How to open a .pdf file using PdfViewer in Android

痞子三分冷 提交于 2019-12-12 01:27:39

问题


I want to open a .pdf file in my Android application Using PdfViewer library. I have downloaded the library file and added it on lib folder. Also i have added permissions on the manifest. Now after browsing the file it will show Loader Dialog "Loading PDF Page" and the file is not displayed. Here is my code.

//To Browse the 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;
    }
}

回答1:


add PDFViewer as library in your project, copy its resources in your as guided in HERE. This is working code for me. I am reading pdf from assets folder. Do customization as your requirment. 1.Download pdf reader project from above link. 2. copy all resources in your project. 3.extend your activity(in which you want to display pdf) from PdfViewerActivity. 4. copy all methods from pdfreader project in yourpdfview 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);
}
}

The methods of yourpdfview activity are

   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; }


来源:https://stackoverflow.com/questions/30045927/how-to-open-a-pdf-file-using-pdfviewer-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!