PDF viewer events
Use the DocumentListener interface to handle document loading, page changes, and clicks. The Catalog app’s SimpleFragmentExample and CustomLayoutExample demonstrate this interface.
Registering a document listener
Register a listener with PdfFragment using addDocumentListener(). A fragment supports multiple document listeners:
fragment.addDocumentListener(myDocumentListener)fragment.addDocumentListener(myDocumentListener);To prevent memory leaks, PdfFragment removes all registered listeners in its #onDetach method.
Unregistering a document listener
To stop receiving events, pass the listener instance to removeDocumentListener(). Unregister listeners when you no longer need them, such as in your activity’s onStop()(opens in a new tab) method:
fragment.removeDocumentListener(myDocumentListener)fragment.removeDocumentListener(myDocumentListener);Document events
Nutrient calls the following DocumentListener methods in response to document events:
- Document loaded — Nutrient calls
onDocumentLoaded(PdfDocument)whenPdfFragmentfinishes loading the document. The callback receives the loadedPdfDocument. - Loading failed — Nutrient calls
onDocumentLoadFailed(Throwable)when loading fails. TheThrowable(opens in a new tab) identifies the cause. Handle the error in your app — for example, by displaying a message. - Page clicked — Nutrient calls
onPageClick(PdfDocument, int, MotionEvent, PointF, Annotation)when a user clicks a page. The callback provides these parameters:- Document — The
PdfDocumentthat the user clicked. - Page index — The
intvalue identifying the clicked page. - Touch event — The
MotionEvent(opens in a new tab) that triggered the click, including screen coordinates and pointer information. - PDF coordinates — The
PointF(opens in a new tab) identifying the touch position in page coordinates. For details, refer to the coordinate space conversion guide. - Annotation — The
Annotationthat the user touched, ornullif the user didn’t touch an annotation. Use this parameter for custom annotation handling.
- Document — The
- Background clicked — Nutrient calls
onDocumentClick()when a user clicks thePdfFragmentbackground outside any page. - Page changed — Nutrient calls
onPageChanged(PdfDocument, int)when the active page changes. The callback provides the activePdfDocumentand the new page index.
Nutrient uses zero-based page indexes. The value 0 identifies the first page, and pageCount - 1 identifies the last page.
Activity callbacks
PdfActivity implements DocumentListener by default. To handle events in a PdfActivity subclass, override the corresponding callback methods:
class MyActivity : PdfActivity() {
override fun onDocumentLoaded(document : PdfDocument) { super.onDocumentLoaded(document) Toast.makeText(this, "Document has been loaded with ${document.pageCount} pages", Toast.LENGTH_SHORT).show() }
override fun onDocumentLoadFailed(exception : Throwable) { super.onDocumentLoadFailed(exception) Toast.makeText(this, "Document loading failed!", Toast.LENGTH_SHORT).show() exception.printStackTrace() }}public class MyActivity extends PdfActivity {
@Override public void onDocumentLoaded(@NonNull PdfDocument document) { super.onDocumentLoaded(document); Toast.makeText(this, String.format("Document has been loaded with %d pages", document.getPageCount()), Toast.LENGTH_SHORT).show(); }
@Override public void onDocumentLoadFailed(Throwable exception) { super.onDocumentLoadFailed(exception); Toast.makeText(this, "Document loading failed!", Toast.LENGTH_SHORT).show(); exception.printStackTrace(); }}