LoginSignup
5
7

More than 5 years have passed since last update.

KitKat以降で利用できるPDF表示ライブラリを試してみた

Posted at

概要

AndroidアプリでのPDF表示はandroid.graphics.pdf.PdfRendererが利用できますが、Lolipop以降のみ利用可能なため、KitKat端末でも表示できる方法を探してみました。
幾つか採用できそうなライブラリがありましたが、動かしてみた範囲で安定しており、導入も容易だった以下を今回は採用しました。

barteksc/AndroidPdfViewer
https://github.com/barteksc/AndroidPdfViewer

使い方

導入

compile 'com.github.barteksc:android-pdf-viewer:2.3.0'

Viewの配置

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

読み込みと表示

PDFView pdfView = (PDFView)findViewById(R.id.pdfView);
pdfView.recycle();
pdfView.fromAsset(String)
    .enableSwipe(true) //スワイプオンオフ
    .swipeHorizontal(false) //縦スクロールでページ切り替え
    .enableDoubletap(true) //ダブルタップでの拡大
    .defaultPage(0) //初期表示ページ。表示ページのstate復旧はこれで
    .onPageChange(onPageChangeListener)//表示ページのstate保持はこれで
    .onPageScroll(onPageScrollListener)
    .onError(onErrorListener)
    .enableAnnotationRendering(false)
    .password(null)
    .scrollHandle(null)
    .load();

サンプル

PDFファイル群を一覧表示して表示するサンプルを作ってみました。

miyanqii/PDFViewerSample
https://github.com/miyanqii/PDFViewerSample

参考

PdfRenderer
https://developer.android.com/reference/android/graphics/pdf/PdfRenderer.html

barteksc/AndroidPdfViewer
https://github.com/barteksc/AndroidPdfViewer

5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7