简体   繁体   中英

How to open PDF in Android

如何从服务器打开pdf文件而不将其保存在设备上而不使用任何第三方应用程序。因为我不希望我的用户下载任何应用程序来使用我的apps.And我不想使用Web视图打开pdf文件。

This method worked in the older versions of android:

In a new activity:

        WebView webview = new WebView(this);
        setContentView(webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setPluginsEnabled(true);
        webview.loadUrl("http://docs.google.com/gview?embedded=true&url=URL_of_PDF/fileName.pdf");
        setContentView(webview);

give internet permission in manifest file.

USE the following method to open a PDF file with already installed 3rd party application.

To open the PDF in application use:

Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

use PdfViewer.jar and making a code like below - copied from here

First.java

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

    File images = Environment.getExternalStorageDirectory();
    imagelist = images.listFiles(new FilenameFilter()
    {  
            public boolean accept(File dir, String name)  
            {  
                    return ((name.endsWith(".pdf")));
            }  
    }); 
    pdflist = new String[imagelist.length]; 
    for(int i = 0;i<imagelist.length;i++)
    {
            pdflist[i] = imagelist[i].getName();
    }
    this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, pdflist));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{
        super.onListItemClick(l, v, position, id);
        String path = imagelist[(int)id].getAbsolutePath();
        openPdfIntent(path);
}

private void openPdfIntent(String path) 
{
    try
    {
      final Intent intent = new Intent(First.this, Second.class);
      intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
      startActivity(intent);
    }
    catch (Exception e) 
    {
      e.printStackTrace();
    }
}

Second.java

public class Second 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;
}
}

Hope this helps you lot. Try this. Don't forgot to add your Second.java in your manifest. Add some drawables whatever it requires in second.java with your drawables. And, Refer the example from GitHub

for this u have to use pdf reader libraries.this link will help u

https://stackoverflow.com/questions/4665957/pdf-parsing-library-for-android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM