Edit Callbacks
Overview
In content editing mode, the ComPDF SDK provides a comprehensive set of callback interfaces to monitor user editing actions within a PDF document. These include editing states for text, images, and forms, as well as undo/redo operations and edit completion events. By leveraging these callbacks, developers can implement custom UI updates, operation logging, or permission control.
Note
This section focuses on edit callbacks and related operations in content editing mode. For annotation-related callbacks, please refer to the previous sections.
1. Obtaining the Edit Manager
CPDFEditManager is the core manager in content editing mode. It is responsible for managing edit operations, undo/redo history, and the overall editing state.
CPDFEditManager editManager = readerView.getEditManager();2. Edit Status Callbacks
OnEditStatusChangeListener is used to listen for changes in the editing state, including when editing begins, undo/redo availability updates, and when exiting edit mode.
Method Description
| Method | Description | Triggered When |
|---|---|---|
| onBegin(int pageIndex) | Editing starts and returns the page index | User enters edit mode for the first time |
| onUndoRedo(int pageIndex, boolean canUndo, boolean canRedo) | Undo/redo state changes | Undo or redo is performed during editing |
| onExit() | Exit edit mode | User finishes editing or endEdit() called |
editManager.addEditStatusChangeListener(new OnEditStatusChangeListener() {
@Override
public void onBegin(int pageIndex) {
// Editing started, e.g. highlight the current editing page
}
@Override
public void onUndoRedo(int pageIndex, boolean canUndo, boolean canRedo) {
// Update undo/redo button states
}
@Override
public void onExit() {
// Editing ended, e.g. hide the edit toolbar or save state
}
});editManager.addEditStatusChangeListener(object : OnEditStatusChangeListener {
override fun onBegin(pageIndex: Int) {
// Editing started, e.g. highlight the current editing page
}
override fun onUndoRedo(pageIndex: Int, canUndo: Boolean, canRedo: Boolean) {
// Update undo/redo button states
}
override fun onExit() {
// Editing ended, e.g. hide the edit toolbar or save state
}
})3. Undo and Redo Operations
CPDFEditManager provides undo and redo APIs to revert or restore user actions during content editing.
editManager.undo(); // Undo the last action
editManager.redo(); // Redo the last action
boolean canUndo = editManager.canUndo(); // Whether undo is available
boolean canRedo = editManager.canRedo(); // Whether redo is availableeditManager.undo() // Undo the last action
editManager.redo() // Redo the last action
val canUndo = editManager.canUndo() // Whether undo is available
val canRedo = editManager.canRedo() // Whether redo is available4. Enable or Disable Editing
You can enable or disable the edit manager to control whether content editing is allowed.
editManager.enable(); // Enable editing
editManager.disable(); // Disable editing
boolean isEnabled = editManager.isEnabled(); // Whether editing is enablededitManager.enable() // Enable editing
editManager.disable() // Disable editing
val isEnabled = editManager.isEnabled() // Whether editing is enabled5. Ending Editing
After editing is completed, call endEdit() to exit edit mode and trigger the onExit() callback.
editManager.endEdit();editManager.endEdit()Edit Area Selection Change Callback
In content editing mode, users can select text, image, or path areas within the document. OnSelectEditAreaChangeListener is used to listen for changes in the selected edit area, allowing developers to perform custom actions such as displaying toolbars, updating property panels, or triggering editing logic.
Method Description
| Method | Description | Triggered When |
|---|---|---|
| onSelectEditAreaChange(int type) | Edit area selection changed | User selects or deselects an edit area |
Edit Area Type Values
| Type Value | Description |
|---|---|
| 0 | No selection (None) |
| 1 | Text area |
| 2 | Image area |
| 3 | Path area (e.g. drawn lines or shapes) |
Retrieving the Selected Edit Area
You can retrieve the currently selected edit area via readerView.getSelectEditArea() and handle it based on its type:
CPDFEditTextArea– Text areaCPDFEditImageArea– Image areaCPDFEditPathArea– Path area
readerView.setSelectEditAreaChangeListener(new OnSelectEditAreaChangeListener() {
@Override
public void onSelectEditAreaChange(int type) {
// type: 0 None, 1 Text, 2 Image, 3 Path
CPDFEditArea editArea = readerView.getSelectEditArea();
if (editArea instanceof CPDFEditTextArea) {
// Text area selected, show text toolbar or property panel
} else if (editArea instanceof CPDFEditImageArea) {
// Image area selected, show image editing tools
} else if (editArea instanceof CPDFEditPathArea) {
// Path area selected, show path properties or delete options
}
}
});readerView.setSelectEditAreaChangeListener { type ->
// type: 0 None, 1 Text, 2 Image, 3 Path
val editArea = readerView.selectEditArea
when (editArea) {
is CPDFEditTextArea -> {
// Text area selected, show text toolbar or property panel
}
is CPDFEditImageArea -> {
// Image area selected, show image editing tools
}
is CPDFEditPathArea -> {
// Path area selected, show path properties or delete options
}
}
}