Skip to content
ComPDF
DemoAPI ReferenceFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub
Guides

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:

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

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

const historyManager = pdfReaderRef.current?._annotationsHistoryManager;

Setting a History Change Listener

Use setOnHistoryStateChangedListener() to register a callback that runs whenever the annotation history state changes, such as after adding an annotation or performing an undo operation:

tsx
historyManager?.setOnHistoryStateChangedListener((canUndo, canRedo) => {
  // Update the undo and redo controls.
});

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 historyManager?.canUndo();
const 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:

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