Android PdfViewer

repository·master·Indexed 27 days ago

https://github.com/dimuthuupe/androidpdfviewer

A library for displaying PDF documents on Android (API 11+) using PdfiumAndroid. It supports animations, gestures, zooming, double-tap, and various page fit policies. The library provides the PDFView component for rendering documents from URIs, Files, Byte Arrays, InputStreams, and Assets, and includes support for 16 KB page sizes for Android 15 compatibility.

Tokens
2.6K
Snippets
9
Records
17
Agent score
91%

What's inside Android PdfViewer

  1. Configure 16 KB Page Size Support in Gradle

    master

    To ensure compatibility with 16 KB page sizes (required for Google Play starting November 1st, 2025), update your build.gradle and gradle.properties files. You must use NDK r28+ and configure packaging options to use uncompressed shared libraries for proper alignment.

    In your module's build.gradle:

    • Set ndkVersion to "28.0.12433566" or higher.
    • Set packagingOptions.jniLibs.useLegacyPackaging to false.

    In gradle.properties:

    • Set android.bundle.enableUncompressedNativeLibs=false.
    • Set android.enableR8.fullMode=true to ensure proper optimization.
    // android-pdf-viewer/build.gradle
    packagingOptions {
        jniLibs {
            useLegacyPackaging false  // Use uncompressed shared libraries for 16 KB alignment
        }
    }
    
    // Enable 16 KB page size support for native libraries
    ndkVersion "28.0.12433566"  // Use NDK r28+ for 16 KB support
    
    // gradle.properties
    android.bundle.enableUncompressedNativeLibs=false
    android.enableR8.fullMode=true
  2. Install Android PdfViewer

    master

    Add the following dependency to your build.gradle file to use the library. You can choose between the latest beta version or a more stable version.

    Beta version (3.2.0-beta.1): implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'

    Stable version (2.8.2): implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

    The library is available in the jcenter repository.

    implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
  3. Test on 16 KB Page Size Devices and Emulators

    master

    To ensure your app works correctly on 16 KB page size environments, use the following methods:

    Android Emulator

    1. Download an Android 15 system image that specifically supports 16 KB page sizes.
    2. Create a virtual device using that image.

    Physical Devices

    Supported devices (running Android 15 QPR1+ or Android 15 QPR2 Beta 2+):

    • Pixel 8 / 8 Pro
    • Pixel 8a
    • Pixel 9 / 9 Pro / 9 Pro XL

    Note: You must enable "Boot with 16KB page size" in the device's Developer Options to test this mode.

  4. Fix 16 KB Alignment Issues in APKs

    master

    If your APK fails the 16 KB alignment checks, you can use the provided realign_apk.bat script to fix it. It is recommended to work on a copy of your APK to avoid file lock issues.

    1. Copy your APK: cp sample/build/outputs/apk/debug/sample-debug.apk sample-debug-copy.apk
    2. Run the realignment script: .\realign_apk.bat "sample-debug-copy.apk"
    3. Verify the fix: zipalign -c -p -v 4 sample-debug-copy.apk
    .\realign_apk.bat "sample-debug-copy.apk"
  5. Include PDFView in your layout

    master

    Add the PDFView component to your XML layout file to provide the container for rendering PDF documents.

    <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
  6. Achieve ViewPager-like scrolling

    master

    To make the PDF scroll through single pages with a fling behavior similar to a ViewPager, use the following combination of settings:

    pdfView.fromAsset("sample.pdf")
        .swipeHorizontal(true)
        .pageSnap(true)
        .autoSpacing(true)
        .pageFling(true)
        .load();
  7. Load a PDF file using PDFView

    master

    Use the PDFView API to load documents from various sources such as URIs, Files, Byte Arrays, InputStreams, Assets, or custom DocumentSource implementations. You can chain configuration methods to customize the viewer behavior (e.g., page selection, gestures, listeners, and fit policies) before calling .load().

    pdfView.fromAsset("sample.pdf")
        .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)
        .onLoad(onLoadCompleteListener)
        .onError(onErrorListener)
        .pageFitPolicy(FitPolicy.WIDTH)
        .load();
  8. Configure Zoom Levels

    master

    The library supports double-tap zooming with three levels: min (default 1), mid (default 1.75), and max (default 3). You can customize these levels using the following methods:

    void setMinZoom(float zoom);
    void setMidZoom(float zoom);
    void setMaxZoom(float zoom);
  9. Configure ScrollHandle

    master

    The ScrollHandle replaces the old ScrollBar. To use it, register an implementation of the ScrollHandle interface using Configurator#scrollHandle().

    You can use the provided DefaultScrollHandle. By default, it is placed on the right (vertical) or bottom (horizontal). To place it on the left or top, use the constructor with a second boolean argument: new DefaultScrollHandle(this, true).

  10. Verify 16 KB APK Alignment

    master

    You can verify if your APK is correctly aligned for 16 KB page sizes using manual commands or the provided scripts in the repository.

    Manual Verification

    1. Check alignment with zipalign:
      zipalign -c -p -v 4 your-app.apk
    2. Check device page size via ADB: Run the following command on a 16 KB device/emulator. It should return 16384:
      adb shell getconf PAGE_SIZE

    Using Repository Scripts

    • Linux/macOS: ./check_16kb_alignment.sh <path_to_apk>
    • Windows PowerShell: .\check_16kb_alignment.ps1 -ApkFile "<path_to_apk>"
    • Windows Batch: .\realign_apk.bat "<path_to_apk>"
    • Python (Cross-platform): python fix_16kb_alignment.py "<path_to_apk>"
    zipalign -c -p -v 4 your-app.apk
    
    adb shell getconf PAGE_SIZE
    # Should return 16384
  11. Set Page Fit Policy

    master

    The library supports three modes for fitting pages to the screen via Configurator#pageFitPolicy(FitPolicy). Every page is scaled relative to other pages based on the selected policy:

    • FitPolicy.WIDTH: The width of the widest page is equal to the screen width (Default).
    • FitPolicy.HEIGHT: The height of the highest page is equal to the screen height.
    • FitPolicy.BOTH: Every page is scaled to be fully visible on screen based on both widest and highest pages.