Skip to content
ComPDF
Guides

Annotation Operation Callbacks

This section describes the callback interfaces provided by CPDFReaderView in annotation mode, including events for selecting, deselecting, adding, and modifying annotations. Developers can use these callbacks to listen to user interactions with annotations, perform custom logic, or update the UI, providing richer control over annotation interactions. This section will be continuously updated to cover callbacks for highlights, ink, stamps, forms, and other annotation types.

Annotation Selection Callback

CPDFSelectAnnotCallback is used to listen for annotation selection and deselection events. Through this callback, developers can execute custom actions when an annotation is selected or deselected, such as showing a property panel, modifying styles, or triggering other UI updates.

Interface Methods

MethodDescriptionTrigger
onAnnotationSelectedAnnotation selectedTriggered when the user taps or selects an annotation
onAnnotationDeselectedAnnotation deselectedTriggered when the user taps an empty area or selects another annotation
java
readerView.setSelectAnnotCallback(new CPDFSelectAnnotCallback() {
    @Override
    public void onAnnotationSelected(CPDFPageView cpdfPageView,
                                     CPDFBaseAnnotImpl<CPDFAnnotation> cpdfBaseAnnot) {
        // Annotation selected: show property panel or highlight selection
    }

    @Override
    public void onAnnotationDeselected(CPDFPageView cpdfPageView,
                                       @Nullable CPDFBaseAnnotImpl<CPDFAnnotation> cpdfBaseAnnot) {
        // Annotation deselected: hide property panel or remove highlight
    }
});
kotlin
readerView.setSelectAnnotCallback(object : CPDFSelectAnnotCallback {
    override fun onAnnotationSelected(
        cpdfPageView: CPDFPageView,
        cpdfBaseAnnot: CPDFBaseAnnotImpl<CPDFAnnotation>
    ) {
        // Annotation selected: show property panel or highlight selection
    }

    override fun onAnnotationDeselected(
        cpdfPageView: CPDFPageView,
        cpdfBaseAnnot: CPDFBaseAnnotImpl<CPDFAnnotation>?
    ) {
        // Annotation deselected: hide property panel or remove highlight
    }
})

Annotation Add Callback

CPDFAddAnnotCallback is used to listen for annotation addition events. Developers can execute custom logic after an annotation is added, such as logging, updating a database, or setting styles for the new annotation.

Interface Methods

MethodDescriptionTrigger
onAddAnnotationAnnotation added to PDFTriggered after a user finishes adding an annotation
java
readerView.setPdfAddAnnotCallback(new CPDFAddAnnotCallback() {
    @Override
    public void onAddAnnotation(CPDFPageView cpdfPageView,
                                CPDFBaseAnnotImpl<CPDFAnnotation> cpdfBaseAnnot) {
        // Annotation added: execute custom logic, e.g., highlight or log
    }
});
kotlin
readerView.setPdfAddAnnotCallback(object : CPDFAddAnnotCallback {
    override fun onAddAnnotation(
        cpdfPageView: CPDFPageView,
        cpdfBaseAnnot: CPDFBaseAnnotImpl<CPDFAnnotation>
    ) {
        // Annotation added: execute custom logic, e.g., highlight or log
    }
})

Undo and Redo Callbacks

CPDFReaderView provides an undo/redo manager (CPDFUndoManager) to track the user’s operation history in the PDF, including annotation additions, deletions, and property modifications. By listening to history changes or directly calling undo/redo methods, developers can implement custom undo/redo functionality, update the UI state, or record operations.

Note: The undo manager is suitable for annotation editing, form filling, ink drawing, and other operations, allowing precise control of user history.

1. Get the Undo Manager

java
CPDFUndoManager manager = readerView.getUndoManager();

2. Undo History Change Listener

OnUndoHistoryChangeListener listens for changes in the operation history, including insertion, deletion, modification, or clearing of operations.

Operation Enum

ValueDescriptionTrigger
INSERTInsert operationNew annotation or form element added
REMOVERemove operationAnnotation or form element removed
ATTRIBUTEAttribute modificationChange color, opacity, size, or other properties
REMOVE_ALLClear all operationsClear all undoable operations in the document

Example

java
manager.addOnUndoHistoryChangeListener(new OnUndoHistoryChangeListener() {
    @Override
    public void onUndoHistoryChanged(@NonNull CPDFUndoManager cpdfUndoManager,
                                     Operation operation,
                                     Type type) {
        // operation: current operation type, e.g., INSERT, REMOVE
        // type: object type (annotation, form, etc.)
        Log.d("UndoManager", "Operation: " + operation + ", Object type: " + type);
    }
});
kotlin
manager.addOnUndoHistoryChangeListener(object : OnUndoHistoryChangeListener {
    override fun onUndoHistoryChanged(cpdfUndoManager: CPDFUndoManager,
                                      operation: Operation,
                                      type: Type) {
        // operation: current operation type, e.g., INSERT, REMOVE
        // type: object type (annotation, form, etc.)
        Log.d("UndoManager", "Operation: $operation, Object type: $type")
    }
})

3. Enable/Disable Undo Manager

java
manager.enable(true);   // Enable undo/redo management
boolean isEnabled = manager.isEnable(); // Check if enabled

4. Perform Undo/Redo

java
manager.undo();      // Undo last operation
manager.redo();      // Redo last undone operation
boolean canRedo = manager.canRedo(); // Can redo?
boolean canUndo = manager.canUndo(); // Can undo?

5. Clear Operation History

java
manager.clearHistory();  // Clear undo/redo history