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.
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' }
}
}
Include the library in your app-level build.gradle
:
dependencies {
implementation 'com.github.iamyashchouhan:AndroidPdfViewer:1.0.3' // Replace 'Tag' with the latest version
}
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"/>
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
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
. UsepdfView.useBestQuality(true)
to switch toARGB_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);
- Default zoom levels are:
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();
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.
This project is licensed under the MIT License. See the LICENSE file for details.
© 2024 iamyashchouhan, YMG-Developers