Skip to content
ComPDF
Guides

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.

java
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

MethodDescriptionTriggered When
onBegin(int pageIndex)Editing starts and returns the page indexUser enters edit mode for the first time
onUndoRedo(int pageIndex, boolean canUndo, boolean canRedo)Undo/redo state changesUndo or redo is performed during editing
onExit()Exit edit modeUser finishes editing or endEdit() called
java
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
    }
});
kotlin
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.

java
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 available
kotlin
editManager.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 available

4. Enable or Disable Editing

You can enable or disable the edit manager to control whether content editing is allowed.

java
editManager.enable();                      // Enable editing
editManager.disable();                     // Disable editing
boolean isEnabled = editManager.isEnabled(); // Whether editing is enabled
kotlin
editManager.enable()                     // Enable editing
editManager.disable()                    // Disable editing
val isEnabled = editManager.isEnabled()  // Whether editing is enabled

5. Ending Editing

After editing is completed, call endEdit() to exit edit mode and trigger the onExit() callback.

java
editManager.endEdit();
kotlin
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

MethodDescriptionTriggered When
onSelectEditAreaChange(int type)Edit area selection changedUser selects or deselects an edit area

Edit Area Type Values

Type ValueDescription
0No selection (None)
1Text area
2Image area
3Path 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 area
  • CPDFEditImageArea – Image area
  • CPDFEditPathArea – Path area
java
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
        }
    }
});
kotlin
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
        }
    }
}