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
| Method | Description | Trigger |
|---|---|---|
| onAnnotationSelected | Annotation selected | Triggered when the user taps or selects an annotation |
| onAnnotationDeselected | Annotation deselected | Triggered when the user taps an empty area or selects another annotation |
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
}
});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
| Method | Description | Trigger |
|---|---|---|
| onAddAnnotation | Annotation added to PDF | Triggered after a user finishes adding an annotation |
readerView.setPdfAddAnnotCallback(new CPDFAddAnnotCallback() {
@Override
public void onAddAnnotation(CPDFPageView cpdfPageView,
CPDFBaseAnnotImpl<CPDFAnnotation> cpdfBaseAnnot) {
// Annotation added: execute custom logic, e.g., highlight or log
}
});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
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
| Value | Description | Trigger |
|---|---|---|
| INSERT | Insert operation | New annotation or form element added |
| REMOVE | Remove operation | Annotation or form element removed |
| ATTRIBUTE | Attribute modification | Change color, opacity, size, or other properties |
| REMOVE_ALL | Clear all operations | Clear all undoable operations in the document |
Example
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);
}
});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
manager.enable(true); // Enable undo/redo management
boolean isEnabled = manager.isEnable(); // Check if enabled4. Perform Undo/Redo
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
manager.clearHistory(); // Clear undo/redo history