Undo/Redo PDF Edits - Python Sample Code

The Apryse 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). This sample code (provided in Python, C++, C#, Java, Node.js, PHP, Ruby, Go and VB) shows how to use Apryse SDK to walk back and forth on a fully general, bit-exact list of document states. Saving changes in a mode that is not 'incremental' will wipe out the undo-redo state list; the API will not be able to access old snapshots anymore. See the undoing and redoing guide for more information. Learn more about our Server SDK.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14# Relative path to the folder containing test files.
15input_path = "../../TestFiles/"
16output_path = "../../TestFiles/Output/"
17
18#---------------------------------------------------------------------------------------
19# The following sample illustrates how to use the UndoRedo API.
20#---------------------------------------------------------------------------------------
21
22def main():
23 # The first step in every application using PDFNet is to initialize the
24 # library and set the path to common PDF resources. The library is usually
25 # initialized only once, but calling Initialize() multiple times is also fine.
26 PDFNet.Initialize(LicenseKey)
27
28 # Open the PDF document.
29 doc = PDFDoc(input_path + "newsletter.pdf")
30
31 undo_manager = doc.GetUndoManager()
32
33 # Take a snapshot to which we can undo after making changes.
34 snap0 = undo_manager.TakeSnapshot()
35
36 snap0_state = snap0.CurrentState()
37
38 # Start a new page
39 page = doc.PageCreate()
40
41 bld = ElementBuilder() # Used to build new Element objects
42 writer = ElementWriter() # Used to write Elements to the page
43 writer.Begin(page) # Begin writing to this page
44
45 # ----------------------------------------------------------
46 # Add JPEG image to the file
47 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
48
49 element = bld.CreateImage(img, Matrix2D(200, 0, 0, 250, 50, 500))
50 writer.WritePlacedElement(element)
51
52 # Finish writing to the page
53 writer.End()
54 doc.PagePushFront(page)
55
56 # Take a snapshot after making changes, so that we can redo later (after undoing first).
57 snap1 = undo_manager.TakeSnapshot()
58
59 if snap1.PreviousState().Equals(snap0_state):
60 print("snap1 previous state equals snap0_state; previous state is correct")
61
62 snap1_state = snap1.CurrentState()
63
64 doc.Save(output_path + "addimage.pdf", SDFDoc.e_incremental)
65
66 if undo_manager.CanUndo():
67 undo_snap = undo_manager.Undo()
68
69 doc.Save(output_path + "addimage_undone.pdf", SDFDoc.e_incremental)
70
71 undo_snap_state = undo_snap.CurrentState()
72
73 if undo_snap_state.Equals(snap0_state):
74 print("undo_snap_state equals snap0_state; undo was successful")
75
76 if undo_manager.CanRedo():
77 redo_snap = undo_manager.Redo()
78
79 doc.Save(output_path + "addimage_redone.pdf", SDFDoc.e_incremental)
80
81 if redo_snap.PreviousState().Equals(undo_snap_state):
82 print("redo_snap previous state equals undo_snap_state; previous state is correct")
83
84 redo_snap_state = redo_snap.CurrentState()
85
86 if redo_snap_state.Equals(snap1_state):
87 print("Snap1 and redo_snap are equal; redo was successful")
88 else:
89 print("Problem encountered - cannot redo.")
90 else:
91 print("Problem encountered - cannot undo.")
92 PDFNet.Terminate()
93
94if __name__ == '__main__':
95 main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales