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.
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.
This page is intended for the following scenarios:
Before starting the integration, confirm that the following items are already in place:
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.
After adding the view to the layout, complete the following steps in order:
CPDFReaderView instance.CPDFDocument.open(...) on the document.CPDFReaderView after 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")
}
}CPDFReaderView is responsible for rendering document content, while CPDFDocument is the object that actually opens the file. The correct sequence is:
CPDFDocument.document.open(...).PDFDocumentErrorSuccess.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.
open(...) failures are usually caused by invalid paths, missing permissions, corrupted files, or inaccessible file sources.Uri, download directory, or sandbox path, verify read access before opening it.