How To Create PDF Reader App In Android
PDF stands for Portable Document Format. It's a file format developed by Adobe that preserves a document's layout, fonts, and images, making it independent of the software, hardware, and operating system used to view or print it. PDFs are commonly used for sharing documents across different platforms while maintaining consistent formatting.
In Android Framework There Is A Built-in Class Called PdfRenderer Which Allow Us To Read PDF Files Introduced In Android 5.0 (API level 21). But For Older Versions Of Android We Don't Have Any Solutions So We Will Use External Libraries To Achieve This. On Of The Library Is Called Barteksc PDF Viewer. In This Tutorial We Will Cover On How We Can Read PDF Files From Asset Folder Using This Library.
Step1. Create Or Open An Existing Android Project.
Step2. Add The Following Library Dependency In Your app-level build.gradle File.
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
Step3. After Downloading The Library Go To The Layout File In Which You Want To Read The PDF File. In My Side I Want To Read The PDF File in activity_main.xml File & Add The PDF View Layout Using This Code.
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfview1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Step4. Then Go To MainActivity.java Or Any Other Activity You Want To Read PDF File & Add The Following Import Statements.
//Add The DefaultScrollHandle Class If You Want To Attach Fast Scrolling Thumb For The PDFView.
import com.github.barteksc.pdfviewer.*;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
Step5. Place A PDF File Inside Your Project Asset Folder With Any Name You Want(Example sample.pdf) & Use The Following Codes.
//First Define The PDFView Layout
PDFView pdfview1 = findViewById(R.id.pdfview1);
//Then Use PDFView fromAsset Method To Read PDF File From Asset Folder
pdfview1.fromAsset("sample.pdf")
//You Can Also Read PDF File From USB Storage Of The Device Using fromFile Method As Follows & Don't Forget To Add Read External Storage Permission To Read PDF File From USB Storage Of The Android Device.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
//Also If Your Project Target SDK Version Is 30 Or Higher Change It To 29 Otherwise You Don't Get What You Want.
pdfview1.fromFile(new File("/storage/emulated/0/sample.pdf"))
//Again The DefaultScrollHandle Is Optional.
.scrollHandle(new DefaultScrollHandle(this, false))
//If You Want To Set The Scroll Handle To The Left Side Of The PDF View Use This Code Instead.
.scrollHandle(new DefaultScrollHandle(this, true))
//Finally Load The PDF File
.load();
Step6. Run Your Project. Now You Will Be Able To Read The PDF File From Your Asset Folder.
If You Want To Download The Project Join Android Developers Telegram Channel.
If You Want To Go Further For Customizing The PDFView You Can Visit The Official GitHub Documentation.
Reference GitHub
Thank You For Reading
How to load from internet?
ReplyDeleteThe Library Doesn't Support Viewing PDF Files From Internet Read The GitHub Documentation To Learn More
ReplyDeletehttps://github.com/barteksc/AndroidPdfViewer