Undo and redo tool for Server/Desktop

The PDFNet SDK has a low-level facility for undo and redo operations. It is a API that applies to any edits made to a particular document (not just annotations). It walks back and forth on a fully general, bit-exact list of document states.

Given a set of changes to a document, the undo-redo API allows one to take snapshots after a group of changes. Then, one may move between these snapshots using the API. In return, the functions return objects representing state changes. (It is also possible for one to retrieve objects representing document states in certain ways, if one finds it necessary to keep track of or compare these.)

A fragment of a concrete example:

1PDFDoc doc = new PDFDoc(filename);
2
3// Retrieve the undo manager.
4UndoManager undo_manager = doc.GetUndoManager();
5
6// Take a snapshot to which we can undo after making changes.
7ResultSnapshot snap0 = undo_manager.TakeSnapshot();
8
9// perform some document processing using read write operations
10// found under 'Editing Page Content' or 'Page Manipulation'
11
12// Take a snapshot after making changes, so that we can redo later (after undoing first).
13ResultSnapshot snap1 = undo_manager.TakeSnapshot();
14
15if (undo_manager.CanUndo())
16{
17 ResultSnapshot undo_snap = undo_manager.Undo();
18
19 if (undo_manager.CanRedo())
20 {
21 ResultSnapshot redo_snap = undo_manager.Redo();
22 }
23}

A low-level facility for undo and redo operations
Full sample code which applies to any edits made to a particular document (not just annotations). It shows how to walk back and forth on a fully general, bit-exact list of document states.

About undo and redo

An important thing to realize about the undo-redo API is that saving in a mode that is not 'incremental' will (among other things) wipe out the undo-redo state list; the API will not be able to access old snapshots anymore. The reason for this is that full saves irrevocably change the entire document, whereas incremental saves append changes to the end (thus allowing undos and redos to be done).

Generally speaking, the undo-redo API is driven by the user via calls to the member functions of the UndoManager object (which is in the ::pdftron::SDF namespace in C++), which is retrieved from the PDFDoc via the GetUndoManager member function. Those functions of UndoManager which are concerned with creating or performing state transitions (TakeSnapshot, Redo, Undo) return ResultSnapshots, which represent state transitions (between document states). There are some other functions that allow retrieval of document states (which are represented by DocSnapshots).

The functions which perform state transitions between snapshots (Undo, Redo) are permissive in that attempting to perform transitions when there are no corresponding snapshots will return an invalid snapshot, allowing the user to check their validity. However, it is also possible to use the functions CanUndo and CanRedo to check for the ability to undo and redo prior to performing the undo or redo operation.

In general, undo-redo workflow takes place as follows:

sh

1Initialize PDFNet
2 Open or create a PDFDoc
3 [
4 [
5 UndoManager TakeSnapshot
6 Make some changes
7 ] (repeat 0-n times)
8
9 [
10 call UndoManager CanUndo to check if it is possible to undo
11 If true, call UndoManager Undo - changes are undone
12 ] (repeat 0-n times)
13
14 [
15 Save incrementally
16 ] (repeat 0-n times)
17
18 [
19 UndoManager TakeSnapshot
20 Make some changes
21 ] (repeat 0-n times)
22
23 [
24 call UndoManager CanRedo to check if it is possible to redo
25 If true, call UndoManager Redo - changes are undone
26 ] (repeat 0-n times)
27
28 [
29 Save incrementally
30 ] (repeat 0-n times)
31 ] (repeat 0-n times)

Miscellaneous information: - Save operations themselves do not create new states in the state list. - UndoManager Undo and Redo operations themselves do not save to disk; it is necessary to call Save on PDFDoc.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales