PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
This section will help you to quickly get started with ComPDF SDK to make an Android app in Java with step-by-step instructions. Through the following steps, you will get a simple application that can display the contents of a specified PDF file.

The first step we need to take is to import the ComPDF SDK. You can integrate it in two ways: using Integrate With Gradle or Integrate Manually.
settings.gradle file located in your project's root directory and add the mavenCentral repository:dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
+ mavenCentral()
}
}build.gradle file in the application module directory:
Edit it and add the complete ComPDF SDK dependency:
dependencies {
implementation 'com.compdf:compdfkit:3.0.1'
implementation 'com.compdf:compdfkit-ui:3.0.1'
}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 permissions to external storage at runtime.
AndroidManifest.xml:<uses-permission android:name="android.permission.INTERNET"/>
...
dependencies {
/*ComPDF SDK*/
implementation(fileTree('libs'))
...
}
...dependencies. For simplicity, update the dependencies 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'
}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 permissions to external storage at runtime.
AndroidManifest.xml:<uses-permission android:name="android.permission.INTERNET"/>For detailed steps, please refer to Apply the License Key.
In the proguard-rules.pro file, please add the obfuscation configuration information for compdfkit as follows:
-keep class com.compdfkit.ui.** {*;}
-keep class com.compdfkit.core.** {*;}

Android Studio will automatically generate a source file called "MainActivity.java" and a layout file called "activity_main.xml".
The source file:

The layout file:

CPDFReaderView in your "activity_main.xml" to display the contents of the PDF document:<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- Create a CPDFReaderView -->
<com.compdfkit.ui.reader.CPDFReaderView
android:id="@+id/readerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>Get the CPDFReaderView from the layout or create a CPDFReaderView directly in the code in the corresponding MainActivity.java file:
// Your MainActivity.java file
package com.compdfkit.pdfviewer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.compdfkit.ui.reader.CPDFReaderView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get CPDFReaderView from xml.
CPDFReaderView readerView = findViewById(R.id.readerview);
// Code to create CPDFReaderView.
// CPDFDocument readerView = new CPDFReaderView(content);
}
}// Your MainActivity.kt file
package com.compdfkit.pdfviewer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.compdfkit.ui.reader.CPDFReaderView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get CPDFReaderView from xml.
val readerView = findViewById<CPDFReaderView>(R.id.readerview)
// Code to create CPDFReaderView.
// CPDFDocument readerView = new CPDFReaderView(content);
}
}// Your MainActivity.java file
... //imports
public class MainActivity extends AppCompatActivity {
// Copy the PDF file from the assets folder to the cache folder.
private void copyPdfFromAssetsToCache(String fileName) {
try {
InputStream inputStream = getAssets().open(fileName);
File outputFile = new File(getCacheDir(), fileName);
FileOutputStream outputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CPDFReaderView readerView = findViewById(R.id.readerview);
// Code to create CPDFReaderView.
// CPDFDocument readerView = new CPDFReaderView(content);
//Create a document object.
CPDFDocument document = new CPDFDocument(this);
new Thread(() -> {
String fileName = "Quick Start Guide.pdf";
copyPdfFromAssetsToCache(fileName);
File file = new File(getCacheDir(), fileName);
String filePath = file.getAbsolutePath();
//Open document.
CPDFDocument.PDFDocumentError error = document.open(filePath);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
//The document is encrypted and requires a password to open.
error = document.open(filePath, "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
}
}).start();
}
}// Your MainActivity.kt file
... //imports
class MainActivity : AppCompatActivity() {
// Copy the PDF file from the assets folder to the cache folder.
private fun copyPdfFromAssetsToCache(fileName: String) {
try {
val inputStream = assets.open(fileName)
val outputFile = File(cacheDir, fileName)
val outputStream = FileOutputStream(outputFile)
val buffer = ByteArray(1024)
var bytesRead: Int
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
outputStream.write(buffer, 0, bytesRead)
}
inputStream.close()
outputStream.flush()
outputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val readerView = findViewById<CPDFReaderView>(R.id.readerview)
// Code to create CPDFReaderView.
// CPDFDocument readerView = new CPDFReaderView(content);
//Create a document object.
val document = CPDFDocument(this)
Thread {
val fileName = "Quick Start Guide.pdf"
copyPdfFromAssetsToCache(fileName)
val file = File(cacheDir, fileName)
val filePath = file.absolutePath
//Open document.
var error = document.open(filePath)
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
//The document is encrypted and requires a password to open.
error = document.open(filePath, "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
}
}.start()
}
}CPDFReaderView :// Your MainActivity.java file
... // imports
public class MainActivity extends AppCompatActivity {
// Create a handler to run the code on the main thread.
private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
...
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
// The document is opened successfully and data can be parsed and manipulated.
mainThreadHandler.post(() -> {
// Set the document content for UI.
readerView.setPDFDocument(document);
});
} else {
// The PDF file failed to open. You can refer to the API file for specific error
}
...
}// Your MainActivity.kt file
... // imports
public class MainActivity : AppCompatActivity() {
// Create a handler to run the code on the main thread.
private var mainThreadHandler = Handler(Looper.getMainLooper());
...
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
// The document is opened successfully and data can be parsed and manipulated.
mainThreadHandler.post {
// Set the document content for UI.
readerView.setPDFDocument(document);
}
} else {
// The PDF file failed to open. You can refer to the API file for specific error
}
...
}// Your MainActivity.java file
... // imports
public class MainActivity extends AppCompatActivity {
// Create a handler to run the code on the main thread.
private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
// Copy the PDF file from the assets folder to the cache folder.
private void copyPdfFromAssetsToCache(String fileName) {
try {
InputStream inputStream = getAssets().open(fileName);
File outputFile = new File(getCacheDir(), fileName);
FileOutputStream outputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CPDFReaderView readerView = findViewById(R.id.readerview);
//Create a document object.
CPDFDocument document = new CPDFDocument(this);
new Thread(() -> {
String fileName = "Quick Start Guide.pdf";
copyPdfFromAssetsToCache(fileName);
File file = new File(getCacheDir(), fileName);
String filePath = file.getAbsolutePath();
//Open document.
CPDFDocument.PDFDocumentError error = document.open(filePath);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
//The document is encrypted and requires a password to open.
error = document.open(filePath, "password");
}
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
//The document is opened successfully and data can be parsed and manipulated.
mainThreadHandler.post(() -> {
//Set the document to the reader view.
readerView.setPDFDocument(document);
});
} else {
//The PDF file failed to open. You can refer to the API file for specific error
}
}).start();
}
}// Your MainActivity.kt file
... // imports
class MainActivity : AppCompatActivity() {
// Create a handler to run the code on the main thread.
private val mainThreadHandler = Handler(Looper.getMainLooper())
// Copy the PDF file from the assets folder to the cache folder.
private fun copyPdfFromAssetsToCache(fileName: String) {
try {
val inputStream = assets.open(fileName)
val outputFile = File(cacheDir, fileName)
val outputStream = FileOutputStream(outputFile)
val buffer = ByteArray(1024)
var bytesRead: Int
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
outputStream.write(buffer, 0, bytesRead)
}
inputStream.close()
outputStream.flush()
outputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val readerView = findViewById<CPDFReaderView>(R.id.readerview)
//Create a document object.
val document = CPDFDocument(this)
Thread {
val fileName = "Quick Start Guide.pdf"
copyPdfFromAssetsToCache(fileName)
val file = File(cacheDir, fileName)
val filePath = file.absolutePath
//Open document.
var error = document.open(filePath)
if (error == PDFDocumentError.PDFDocumentErrorPassword) {
//The document is encrypted and requires a password to open.
error = document.open(filePath, "password")
}
if (error == PDFDocumentError.PDFDocumentErrorSuccess) {
//The document is opened successfully and data can be parsed and manipulated.
mainThreadHandler.post {
//Set the document to the reader view.
readerView.pdfDocument = document
}
} else {
//The PDF file failed to open. You can refer to the API file for specific error
}
}.start()
}
}<!-- Your activity_main.xml file -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.compdfkit.ui.reader.CPDFReaderView
android:id="@+id/readerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now, with the help of ComPDF, you can get a simple application to display a PDF file.
Unable to Open the PDF Files
The license we provide to you is tied to your application ID, so ensure that the license acquired matches your application ID.
Other Problems
If you meet some other problems when integrating our ComPDF SDK for Android, feel free to contact ComPDF team.