Undo and Redo in Processing PDFs with Java

By ComPDFKit | 2023 Jun 30
Java Annotations

Portable Document Format is a widely used file format to share documents. It is a non-editable format by default, but there are tools available to edit the PDF content. When it comes to editing, undo and redo are one of the basic features users expect. In this article, we will talk about different ways to undo and redo processes in editing PDFs and how ComPDFKit implements it in Java.

Different Ways to Undo & Redo


When it comes to implementing undo and redo features, there are several approaches available.


One such approach is the Memento Pattern. This popular pattern captures the current state of an object, which can be restored later. Its advantages include the ability to maintain encapsulation, as well as the ability to store multiple states for an object.


Another approach is the Command Pattern, which encapsulates a request as an object and enables executing, undoing, and redoing of requests. This pattern is especially useful when multiple operations need to be combined into a single request. Moreover, it is a better way to decrease the space required for data storage.


The third approach is the Event Sourcing Pattern, which captures and stores every event that occurs on an object. This pattern allows undoing and redoing by replaying the events in reverse order. Undoing an action using event sourcing simply means sending the command with the opposite action, making it an intuitive and straightforward approach to implementing undo and redo features.

How to Undo and Redo with ComPDFKit Java Library


ComPDFKit implements undo and redo features by the Command Pattern because of its small storage space required, and provides undo and redo in several parts such as PDF editing, annotation, etc. Here will show how ComPDFKit undo/redo in Java.


Undo & Redo in PDF Editing Mode


ComPDFKit provides a document object which represents the PDF document. The document object has an EditManager which tracks changes to the content of the PDF document. When a change is made, it is recorded in the EditManager. To undo the change, the document object calls the undo method of the EditManager. Similarly, to redo the change, the document object calls the redo method of the EditManager.


Here are the code samples to undo the two previous actions on the current page. The first of these actions is to change the black text to red, and the second is to increase the font size from 12px to 16px.


CPDFEditManager editManager = cpdfReaderView.getEditManager();
int undoPage = editManager.undo();
int redoPage = editManager.redo();


Undo & Redo in Annotation


ComPDFKit provides an undo and redo manager in the annotation object to undo or redo annotations. It is similar to undo/redo in PDF editing mode. The following code shows how to redo the process of creating line annotation with a square line start and end. 


if (readerView.getUndoManager().canRedo()) {
    try {
    } catch (CPDFRedoFailedException e) {



Undo and redo are essential features when it comes to editing documents. ComPDFKit provides a simple and effective way to implement undo and redo in Java. It uses an undo manager to track changes and allows to undo and redo changes with a single API call. This makes it easy for developers to implement undo and redo in their PDF applications.