Skip to content

Annotation History Management

The CPDFAnnotationHistoryManager class manages the annotation editing history within the current document. It provides undo and redo functionality, allowing users to revert or restore actions such as adding, modifying, or deleting annotations.

Accessing the Annotation History Manager

You can obtain an instance of CPDFAnnotationHistoryManager through the CPDFReaderWidgetController:

dart
CPDFReaderWidgetController? _controller;
// Initialize CPDFReaderWidget and obtain the controller in the onCreated callback
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
);

CPDFAnnotationHistoryManager historyManager = _controller.annotationHistoryManager;

Setting a History Change Listener

Use setOnHistoryChangedListener() to set a callback that will be triggered whenever the annotation history state changes (e.g., after a new annotation is added or an undo action is performed):

dart
historyManager.setOnHistoryChangedListener((canUndo, canRedo) {
  print('Can Undo: $canUndo, Can Redo: $canRedo');
  // Update undo/redo button states accordingly
});

Checking Whether Undo or Redo Is Available

You can check whether there are actions that can be undone or redone using the following methods:

dart
bool canUndo = await historyManager.canUndo();
bool canRedo = await historyManager.canRedo();

These methods return boolean values indicating whether corresponding history actions are available.

Performing Undo and Redo Operations

If there are valid undo or redo actions available, you can perform them as follows:

dart
await historyManager.undo(); // Undo the last annotation action
await historyManager.redo(); // Redo the last undone annotation action