Android
ComPDFKit PDF SDK
Get Started

How to Make an Android App in Java With ComPDFKit PDF SDK

 

This section will help you to quickly get started with ComPDFKit PDF SDK to make an Android app in Java with step-by-step instructions, which includes the following steps:

 

- Use Android Studio to create a Phone & Tablet project.

- Copy "ComPDFKit.aar" and "ComPDFKit-UI.aar" to the "libs" directory of the app.

- Add the following code into the app’s build.gradle file:

 

android{
repositories {
       flatDir {
       /* Folder path where aar is located */
           dirs 'libs'  
      }
}
compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
  }
...
}
dependencies {
...
/*ComPDFKit SDK*/
implementation(name: 'ComPDFKit', ext: 'aar')
implementation(name: 'ComPDFKit-UI', ext: 'aar')
}

 

- Include ComPDFKit PDF SDK for Android as a dependency in the project. Inside the app’s build.gradle, add "ComPDFKit.aar", "ComPDFKit-UI.aar" and the related support libraries to the dependencies. For simplicity, update the dependencies for example as follows:

 

dependencies {
  ...
   //glide
   implementation 'com.github.bumptech.glide:glide:4.12.0'
   annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

   implementation 'androidx.documentfile:documentfile:1.0.1'
}

 

- Apply for read and write permissions in AndroidManifest.xml:

 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

 

Note: On your apps that target Android 6.0 or higher, make sure to check for and request read and write permission to external storage at runtime.

 

- Add this license in the AndroidManifest.xml of the main module:

 

   <meta-data
        android:name="compdfkit_license"
        android:value="{your ComPDFKit license}" />
    <meta-data
        android:name="compdfkit_message"
        android:value="{your ComPDFKit message}" /> />

 

- Copy a PDF document into the assets directory of your Android project — for example, to src/main/assets/Quick Start Guide.pdf.

 

- Open the document. This is a time-consuming process, so it needs to be executed in a sub-thread. After the document is opened successfully, the UI that renders the PDF is initiated:

 

CPDFDocument document = new CPDFDocument(context);
//Open document.
CPDFDocument.PDFDocumentError error = document.open(pdfUri);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
 //The document is encrypted and requires a password to open.
 error = document.open(pdfUri, "password");
}
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
 //The document is opened successfully and data can be parsed and manipulated.
} else {
 //The PDF file failed to open. You can refer to the API file for specific error    messages.
}

 

- Create a CPDFReaderView in layout.xml to display the contents of the PDF document or create a CPDFReaderView directly in the code:

CPDFReaderView readerView;
// Get CPDFReaderView from xml
readerView = findViewById(R.id.readerview);
// Code to create CPDFReaderView
//readerView = new CPDFReaderView(context);

 

- Set the basic properties of CPDFReaderView:

 

//Set the PDF document type to CPDFReaderView
readerView.setPDFDocument(document);