【问题标题】:How to integrate a interactive pdf viewer with in the android application如何在 android 应用程序中集成交互式 pdf 查看器
【发布时间】:2014-06-10 05:54:53
【问题描述】:

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

【问题讨论】:

    标签: android pdf assets interactive


    【解决方案1】:

    您可以使用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 帖子。

    祝你好运!

    【讨论】:

    • 你真的把这种集成称为 PDF 阅读器吗?谢谢
    【解决方案2】:

    对我有帮助:

    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

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-19
      • 2015-12-02
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 2011-04-18
      相关资源
      最近更新 更多