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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales