如何在Android应用程序中集成交互式pdf查看器

时间:2014-06-10 05:54:53

标签: android pdf assets interactive

我无法从资源文件夹中读取PDF文件。我如何在一个页面中阅读PDF,并集成了PDF查看器可用的所有控件。我不想打开手机中提供的默认pdf查看器。请建议我在Android应用程序中实现交互式PDF查看器的正确方法,该应用程序可以从android资源组件中读取assets / raw文件夹中的pdf。提前致谢

2 个答案:

答案 0 :(得分:0)

您可以使用Intent在外部应用程序中加载PDF。

File file = //Load your file from assets here
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

您可以使用InputStream从资源加载文件。请参阅this帖子。

祝你好运!

答案 1 :(得分:0)

这对我有帮助:

Barteksc Android Pdf SDK 库

<块引用>

Android PdfViewer AndroidPdfViewer 1.x 可在 AndroidPdfViewerV1 repo,这里可以独立开发。版本 1.x 使用不同的引擎在画布上绘制文档,因此如果您不喜欢 2.x 版本,请尝试 1.x。

用于在 Android 上显示 PDF 文档的库,带有动画, 手势、缩放和双击支持。它基于 PdfiumAndroid 用于解码PDF文件。适用于 API 11 (Android 3.0) 及更高版本。 根据 Apache 许可证 2.0 获得许可。

支持 Android 4.4.4 (KitKat) 及以上版本的 Android 设备

安装

添加到 build.gradle:

implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'

或者如果您想使用更稳定的版本:

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

将此添加到您的布局文件中:

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

您可以像这样加载 PDF 文件:

pdfView.fromUri(Uri)
or
pdfView.fromFile(File)
or
pdfView.fromBytes(byte[])
or
pdfView.fromStream(InputStream) // stream is written to bytearray - native code cannot use Java Streams
or
pdfView.fromSource(DocumentSource)
or
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();

更多信息Github: Barteksc Android Pdf Viewer

希望对您有所帮助!