PDF查看器的Android实现

jaxagkaj  于 2023-02-11  发布在  Android
关注(0)|答案(2)|浏览(189)

在这里,我需要在我的应用程序中使用PDF查看器。我采取了许多API作为参考,但仍然坚持在查看PDF文件从SDCARD。这里我的代码
'第一个.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();
    }
}}

第二个.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-Libraryproject.in我的模拟器对话框作为“加载PDF文件”点击PDF文件后出现。你们谁能告诉我如何通过手指滑动来更改PDF页面?

nmpmafwu

nmpmafwu1#

你可以在谷歌文档上打开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);

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

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

ax6ht2ek2#

您可以使用AndroidPdfViewer库。

在您的等级中添加此依赖关系:

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

在activity_main.xml中添加PDF视图

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

添加在www.example.com中加载PDF文件MainActivity.java

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.**

相关问题