Using CPDFReaderView
CPDFReaderView is the viewer component provided by ComPDF Android PDF SDK for displaying PDF content inside an Android screen. This page covers the minimum integration flow, including layout setup, instance binding, and document attachment.
Use this page when
This page is intended for the following scenarios:
- Embedding a PDF viewer in an Activity or Fragment.
- Opening a local PDF file and displaying its content.
- Establishing the base integration for later viewer state, navigation, search, and mode configuration.
Complete these prerequisites first
Before starting the integration, confirm that the following items are already in place:
- SDK integration is complete.
- License configuration is valid.
- The current screen has access to a readable PDF file path or source.
- The screen layout is ready to host the viewer component.
Add CPDFReaderView to the layout
Add CPDFReaderView to the screen layout first.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>In screens that also include toolbars, bottom bars, or business UI panels, CPDFReaderView is usually placed in the primary reading area rather than assigned a fixed size.
Get the viewer instance and open the document
After adding the view to the layout, complete the following steps in order:
- Get the
CPDFReaderViewinstance. - Create
CPDFDocument. - Call
open(...)on the document. - Bind the document to
CPDFReaderViewafter the open call succeeds.
The following example shows a minimum working setup.
private CPDFReaderView readerView;
private CPDFDocument document;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
readerView = findViewById(R.id.readerView);
String filePath = "/storage/emulated/0/Download/sample.pdf";
document = new CPDFDocument(this);
PDFDocumentError error = document.open(filePath);
if (error == PDFDocumentError.PDFDocumentErrorSuccess) {
readerView.setPDFDocument(document);
} else {
Log.e("ComPDF", "Open document failed: " + error);
}
}private lateinit var readerView: CPDFReaderView
private var document: CPDFDocument? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
readerView = findViewById(R.id.readerView)
val filePath = "/storage/emulated/0/Download/sample.pdf"
val pdfDocument = CPDFDocument(this)
val error = pdfDocument.open(filePath)
if (error == PDFDocumentError.PDFDocumentErrorSuccess) {
document = pdfDocument
readerView.setPDFDocument(pdfDocument)
} else {
Log.e("ComPDF", "Open document failed: $error")
}
}Follow the integration sequence
CPDFReaderView is responsible for rendering document content, while CPDFDocument is the object that actually opens the file. The correct sequence is:
- Create
CPDFDocument. - Call
document.open(...). - Check whether the result is
PDFDocumentErrorSuccess. - Call
readerView.setPDFDocument(document).
If the document is bound to the viewer before it is opened successfully, the screen will not enter a valid display state.
Keep these points in mind
open(...)failures are usually caused by invalid paths, missing permissions, corrupted files, or inaccessible file sources.- If the document comes from a
Uri, download directory, or sandbox path, verify read access before opening it. - It is usually easier to configure display modes, navigation, search, and themes after the document is displayed successfully.
- Production screens generally also need document loading state and error handling logic.
Related pages
- For document loading state, error prompts, and viewer interaction events, see Document State and Error Callbacks.
- For single-page, double-page, and scroll direction settings, see Display Modes.