Hello there, today am going to walk you through steps with which we will open a PDF File in your app programmatically using Kotlin as our back-end programming language. The PDF File we will open up will be selected from a mobile device (or your emulator say) and then load that on your app and finally open it up.
Here's our road map
- We will select a PDF file from the device
- We will use PSPDFKit to open the selected PDF, so we definitely need to set it up the first thing.
Let's get this started,
First of all, you really need to pardon my ways of listing stuff. Am well aware that i mentioned setting up PSPDFKit a little bit later but it's actually the first thing we will set up. But before we get all deep into this thing, let's get a liitle bit familiar with PSPDFKit. What the heck is it?, keep reading.
PSPDFKit for Android is an SDK for viewing, annotating, editing, and creating PDFs. It offers developers powerful Kotlin and Java APIs for quickly adding PDF functionality to any Android application. For more information, you can visit the website.
Setting up PSPDFKit
Create a New Project (it's more nice if you create using an empty activity template). And with that, we are going to integrate the library right into our build.gradle. The process is extremely easy i promise you that. Just follow the below steps, and we will do just fine.
Step 1:
Navigate to gradle files location, and there find a file named as settings.gradle. It's this place where we are going to add some stuff in the repository. So copy the following code in there
repositories {
google()
mavenCentral()
maven {
url = uri("https://customers.pspdfkit.com/maven")
}
}sometimes, you may get confused as you might find two repositories in there and ask yourself which one is which ?. Well short and simple answer to that question is.. Choose the one at the bottom of the other
Step 2:
Move to your build.gradle at the app level (Not project one) and in there, you should navigate to a point where dependencies are populated then add the following implementation in there
implementation("com.pspdfkit:pspdfkit:8.4.0")Android studio should be asking you to syc your project to this point. But before you do that, am gonna go ahead and remind you something very obvious if you are using the latest android studio, but still i think it's like very important.
PSPDFKIt happens to be supported on devices of API level 21 and above, and it currently targets up to API level 31. So you really should take that into consideration plus you should enable Java 8 language features in the building of your projects. I sometimes find my words really messed up to understand so am going to go ahead and summarize this in code, coz am sure it will be more easier to grasp. Just try and make sure you have the following in your app/ build.gradle
android {
compileSdk = 31
buildToolsVersion = "30.0.3"
defaultConfig {
applicationId = "com.example.app"
minSdk = 21
targetSdk = 31
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
Having all that into one piece, you go ahead and syc your project. Just a quick reminder, you need an active internet connection for this action.
Selecting a PDF file from device
Picking up anything from anyone's device into your app requires permission in most cases, So am very happy to remind you to request READ_EXTERNAL_STORAGE PERMISSION as the first thing. I have an article about this, you can check take a look at it here . But sometimes am usually very bored and really don't feel like writing a lot of stuff just yet, so am going to share a trick with you for personal use that's very cool to use for asking permissions a little bit faster (you'll still need to ask for the permission in code sometimes later).
Cool trick
Navigate to your app's AndroidManifest.xml and add the following
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
After adding that, build you project and as it launches, in your device move to the specific app and into the app info and you should see something like this.
Your testing device could be different from mine, so you may have a bit more nicer interface compared to mine, but there has to be a 90% similarity. Go ahead and click on permissions and flip on the switch to allow READ permission on the app.
Up to that point, congrats! you have granted your app with READ_EXTERNAL_STORAGE manually (not a very professional thing, but faster for temporary use)
Now that we have the permission, let's go a step closer to picking up the PDF File from device programmatically. We are going to use intents to achieve this. You can take a look at a method i designed to do this very specific task in a more cleaner way.
private fun pdfFetcher(){
val soft_nia = Intent(Intent.ACTION_GET_CONTENT)
soft_nia.type = "application/pdf"
soft_nia.addCategory(Intent.CATEGORY_OPENABLE)
mastersoftonia.launch(soft_nia)
}
In there, i have given instructions to to open up a picking Activity and also filter the user selections on the type of file to be selected where the user will only be able to select PDF Files. Something still looks strange in the code above. What exactly is mastersoftonia?, well that's just a random name i picked to define something very crucial in the code flow. Am saying it's crucial because, only if you define it, will the method above work. So make a space somewhere below the method and define the following too.
private var mastersoftonia = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
if (it.resultCode == Activity.RESULT_OK){
if (it.data != null){
anuani = it.data?.data!! //this is the feedback of the selected pdf file
//anuani is a variable i defined to be global area to record the uri of the selected pdf
}
}
}
We have uri of the PDF file in place, it's time to open it using our app. So we definitely need to have an Activity to do this. Let's see how we can do this.
OPENING A PDF FILE PROGRAMMATICALLY
It's high time we see the real magic, opening our selected PDF file using an already built-in PdfActivity (thanks to the PSPDFKit we integrated earlier in our app). To do this you only have to do the following.
Step 1:
Make your app aware of the PdfActivity that is going to handle opening stuff simply by moving to AndroidManifest.xml and add the following lines of code, exactly as they are
<application> <activity android:name="com.pspdfkit.ui.PdfActivity" android:windowSoftInputMode="adjustNothing" /> </application>
Step 2:
On your MainActivity you can then perform a corresponding opening of the selected PDF file using the PdfActivity we just declared in the AndroidManifest. Something to note is that we need to prepare some configurations to do this, plus the uri of the PDF file. I have digested all this for you right below
private var mastersoftonia = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
if (it.resultCode == Activity.RESULT_OK){
if (it.data != null){
val anuani = it.data?.data!! //this is the actual uri of the selected PDF file
val config = PdfActivityConfiguration.Builder(context).build() PdfActivity.showDocument(this, anuani, config) }
}
}
The code above is repetitive, i just modified the code i have shown you in a couple of paragraphs before, so you really don't need to redefine the code. You only have to modify the previous code with lines that we have added right now.
That marks the end of our short and hopefully simple illustration. I really hope this was helpful, leave me a comment about anything you would like me to share next and have a good time coding.
#Stanley


0 Comments