Android代码实现PDF查看器

时间:2012-07-16 07:37:01

标签: android pdf

这里我需要在我的app.i中使用Pdf-Viewer。许多API作为参考,但仍然坚持从SDCARD查看Pdf文件。这是我的代码

`的 First.java

public class First extends ListActivity {
String[] pdflist;
File[] imagelist;
@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));
}
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 int getPreviousPageImageResource() {
    // TODO Auto-generated method stub
    return R.drawable.left_arrow;
}

@Override
public int getNextPageImageResource() {
    // TODO Auto-generated method stub
    return R.drawable.right_arrow;
}

@Override
public int getZoomInImageResource() {
    // TODO Auto-generated method stub
     return R.drawable.zoom_in;
}

@Override
public int getZoomOutImageResource() {
    // TODO Auto-generated method stub
    return R.drawable.zoom_out;
}

@Override
public int getPdfPasswordLayoutResource() {
    // TODO Auto-generated method stub
    return R.layout.pdf_file_password;  
    }

@Override
public int getPdfPageNumberResource() {
    // TODO Auto-generated method stub
     return R.layout.dialog_pagenumber;
}

@Override
public int getPdfPasswordEditField() {
    // TODO Auto-generated method stub
    return R.id.etPassword;
}

@Override
public int getPdfPasswordOkButton() {
    // TODO Auto-generated method stub
     return R.id.btOK;
}

@Override
public int getPdfPasswordExitButton() {
    // TODO Auto-generated method stub
     return R.id.btExit;
}

@Override
public int getPdfPageNumberEditField() {
    // TODO Auto-generated method stub
    return R.id.pagenum_edit;
}}

我使用Android-Pdf-Reader-Library项目。在我的模拟器对话中,点击Pdf文件后出现“正在加载pdf文件”。你能告诉我如何用手指轻扫改变Pdf页面吗?

2 个答案:

答案 0 :(得分:1)

你可以在谷歌文档上打开pdf

    WebView webviewer = new WebView(this);
    webviewer.getSettings().setJavaScriptEnabled(true);
    webviewer.getSettings().setPluginsEnabled(true);
    setContentView(webviewer);

    webviewer.loadUrl("http://docs.google.com/gview?embedded=true&url=PDF_url/fileName.pdf");
    setContentView(webviewer);

要在应用程序中打开PDF,例如quick pdf或adobe pdf reader:

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

答案 1 :(得分:0)

您可以使用AndroidPdfViewer库。

在您的成绩中添加此依存关系:

implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

在activity_main.xml中添加PDFView

<com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

添加在MainActivity.java中加载PDF文件

pdfView.fromUri(Uri)

pdfView.fromFile(File)

pdfView.fromBytes(byte[])

pdfView.fromStream(InputStream) // stream is written to bytearray - native code cannot use Java Streams

pdfView.fromSource(DocumentSource)

pdfView.fromAsset(String)
    .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
    .enableSwipe(true) // allows to block changing pages using swipe
    .swipeHorizontal(false)
    .enableDoubletap(true)
    .defaultPage(0)
    // allows to draw something on the current page, usually visible in the middle of the screen
    .onDraw(onDrawListener)
    // allows to draw something on all pages, separately for every page. Called only for visible pages
    .onDrawAll(onDrawListener)
    .onLoad(onLoadCompleteListener) // called after document is loaded and starts to be rendered
    .onPageChange(onPageChangeListener)
    .onPageScroll(onPageScrollListener)
    .onError(onErrorListener)
    .onPageError(onPageErrorListener)
    .onRender(onRenderListener) // called after document is rendered for the first time
    // called on single tap, return true if handled, false to toggle scroll handle visibility
    .onTap(onTapListener)
    .onLongPress(onLongPressListener)
    .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
    .password(null)
    .scrollHandle(null)
    .enableAntialiasing(true) // improve rendering a little bit on low-res screens
    // spacing between pages in dp. To define spacing color, set view background
    .spacing(0)
    .autoSpacing(false) // add dynamic spacing to fit each page on its own on the screen
    .linkHandler(DefaultLinkHandler)
    .pageFitPolicy(FitPolicy.WIDTH) // mode to fit pages in the view
    .fitEachPage(false) // fit each page to the view, else smaller pages are scaled relative to largest page.
    .pageSnap(false) // snap pages to screen boundaries
    .pageFling(false) // make a fling change only a single page like ViewPager
    .nightMode(false) // toggle night mode
    .load();

如果您使用的是ProGuard ,请不要忘记添加以下行:

-keep class com.shockwave.**