Skip to content

Android view for displaying PDFs rendered with PdfiumAndroid

Notifications You must be signed in to change notification settings

iamyashchouhan/AndroidPdfViewer

Repository files navigation

Android PdfViewer

A powerful library for displaying PDF documents on Android, featuring animations, gestures, zoom, and double-tap support. This library is based on PdfiumAndroid for efficient PDF decoding.

Installation

Step 1: Add Repositories

Add the following to your root build.gradle file to include the necessary repositories:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

Step 2: Add Dependency

Include the library in your app-level build.gradle:

dependencies {
    implementation 'com.github.iamyashchouhan:AndroidPdfViewer:1.0.3' // Replace 'Tag' with the latest version
}

Usage

Include PDFView in Your Layout

Add the PDFView component to your XML layout:

<com.ymg.pdf.viewer.PDFView
    android:id="@+id/pdfView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Load a PDF File

You can load a PDF file using various methods. Here are the available options:

pdfView.fromUri(Uri uri) // Load from a URI
    .fromFile(File file) // Load from a file
    .fromBytes(byte[] bytes) // Load from byte array
    .fromStream(InputStream stream) // Load from InputStream
    .fromSource(DocumentSource source) // Load from a DocumentSource
    .fromAsset("filename.pdf") // Load from assets
    .pages(0, 2, 1, 3, 3, 3) // Specify which pages to display
    .enableSwipe(true) // Enable swipe for page navigation
    .swipeHorizontal(false) // Set swipe direction
    .enableDoubletap(true) // Enable double-tap to zoom
    .defaultPage(0) // Set the default page to display
    .onDraw(onDrawListener) // Callback for custom drawing
    .onLoad(onLoadCompleteListener) // Callback when loading is complete
    .onPageChange(onPageChangeListener) // Callback for page changes
    .onError(onErrorListener) // Callback for errors
    .load(); // Trigger the loading

Additional Options

You can customize the behavior and appearance of the PDF viewer with these options:

  • Bitmap Quality: By default, the generated bitmaps are compressed with RGB_565. Use pdfView.useBestQuality(true) to switch to ARGB_8888.

  • Double Tap Zooming:

    • Default zoom levels are:
      • Min: 1.0
      • Mid: 1.75
      • Max: 3.0
    • You can customize the zoom levels using:
      pdfView.setMinZoom(float zoom);
      pdfView.setMidZoom(float zoom);
      pdfView.setMaxZoom(float zoom);

Example Configuration

Here’s a complete example to get you started:

PDFView pdfView = findViewById(R.id.pdfView);
pdfView.fromAsset("sample.pdf")
    .enableSwipe(true)
    .swipeHorizontal(false)
    .enableDoubletap(true)
    .defaultPage(0)
    .onLoad(new OnLoadCompleteListener() {
        @Override
        public void loadComplete(int nbPages) {
            // Handle loading completion
        }
    })
    .load();

Conclusion

This library simplifies the process of displaying PDF documents in your Android applications while offering robust features and customization options. For more details, feel free to explore the documentation or contribute to the project.

License

This project is licensed under the MIT License. See the LICENSE file for details.

© 2024 iamyashchouhan, YMG-Developers

About

Android view for displaying PDFs rendered with PdfiumAndroid

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages