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 CPDFReaderView:

dart
const pdfReaderRef = useRef<CPDFReaderView>(null);

<CPDFReaderView
  ref={pdfReaderRef}
	document={samplePDF}
	configuration={ComPDFKit.getDefaultConfig({
	})}/>

const historyManager = pdfReaderRef._annotationsHistoryManager;

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):

tsx
pdfReader._annotationsHistoryManager.setOnHistoryStateChangedListener((canUndo, canRedo) => {
  
});

Checking Whether Undo or Redo Is Available

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

tsx
const canUndo = await _annotationsHistoryManager.canUndo();
const canRedo = await _annotationsHistoryManager.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:

tsx
await _annotationsHistoryManager.undo(); // Undo the last annotation action
await _annotationsHistoryManager.redo(); // Redo the last undone annotation action