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 C# code 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 UWP SDK.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11using pdftron.SDF;
12
13using PDFNetUniversalSamples.ViewModels;
14using pdftron.Common;
15
16namespace PDFNetSamples
17{
18 public sealed class UndoRedoTest : Sample
19 {
20 public UndoRedoTest() :
21 base("UndoRedo", "The following sample illustrates how to use the UndoRedo API.")
22 {
23 }
24
25 public override IAsyncAction RunAsync()
26 {
27 return Task.Run(new System.Action(async () => {
28 WriteLine("--------------------------------");
29 WriteLine("Starting UndoRedo Test...");
30 WriteLine("--------------------------------\n");
31
32 bool error = await UndoRedoTestAsync();
33
34 if (error)
35 {
36 WriteLine("UndoRedo failed");
37 }
38 else
39 {
40 WriteLine("UndoRedo succeeded");
41 }
42
43 WriteLine("\n--------------------------------");
44 WriteLine("Done UndoRedo Test.");
45 WriteLine("--------------------------------\n");
46 })).AsAsyncAction();
47 }
48
49 private async Task<bool> UndoRedoTestAsync()
50 {
51 bool error = false;
52
53 // Relative path to the folder containing test files.
54 string inputPath = Path.Combine(InputPath, "newsletter.pdf");
55 string inputImagePath = Path.Combine(InputPath, "peppers.jpg");
56 string outputPath = Path.Combine(OutputPath, "addimage.pdf");
57 string outputUndoPath = Path.Combine(OutputPath, "addimage_undone.pdf");
58 string outputRedoPath = Path.Combine(OutputPath, "addimage_redone.pdf");
59
60 try
61 {
62 // Open the PDF document.
63 PDFDoc document = new PDFDoc(inputPath);
64 ElementBuilder elementBuilder = new ElementBuilder(); // Used to build new Element objects
65 ElementWriter elementWriter = new ElementWriter(); // Used to write Elements to the page
66
67 UndoManager undoManager = document.GetUndoManager();
68
69 // Take a snapshot to which we can undo after making changes.
70 ResultSnapshot snapshot0 = undoManager.TakeSnapshot();
71 DocSnapshot snapshot0State = snapshot0.CurrentState();
72
73 Page page = document.PageCreate(); // Start a new page
74
75 elementWriter.Begin(page); // Begin writing to this page
76
77 // ----------------------------------------------------------
78 // Add JPEG image to the file
79 Image image = Image.Create(document.GetSDFDoc(), inputImagePath);
80 Element element = elementBuilder.CreateImage(image, new Matrix2D(200, 0, 0, 250, 50, 500));
81 elementWriter.WritePlacedElement(element);
82 elementWriter.End(); // Finish writing to the page
83 document.PagePushFront(page);
84
85 // Take a snapshot after making changes, so that we can redo later (after undoing first).
86 ResultSnapshot snapshot1 = undoManager.TakeSnapshot();
87 if (snapshot1.PreviousState().Equals(snapshot0State))
88 {
89 WriteLine("snapshot1 previous state equals snapshot0State; previous state is correct");
90 }
91
92 DocSnapshot snapshot1State = snapshot1.CurrentState();
93
94 await document.SaveAsync(outputPath, SDFDocSaveOptions.e_incremental);
95 await AddFileToOutputList(outputPath).ConfigureAwait(false);
96
97 if (undoManager.CanUndo())
98 {
99 ResultSnapshot undoSnapshot = undoManager.Undo();
100
101 await document.SaveAsync(outputUndoPath, SDFDocSaveOptions.e_incremental);
102 await AddFileToOutputList(outputUndoPath).ConfigureAwait(false);
103
104 DocSnapshot undoSnapshotState = undoSnapshot.CurrentState();
105 if (undoSnapshotState.Equals(snapshot0State))
106 {
107 WriteLine("undoSnapshotState equals snapshot0State; undo was successful");
108 }
109
110 if (undoManager.CanRedo())
111 {
112 ResultSnapshot redoSnapshot = undoManager.Redo();
113
114 await document.SaveAsync(outputRedoPath, SDFDocSaveOptions.e_incremental);
115 await AddFileToOutputList(outputRedoPath).ConfigureAwait(false);
116
117 if (redoSnapshot.PreviousState().Equals(undoSnapshotState))
118 {
119 WriteLine("redoSnapshot previous state equals undoSnapshotState; previous state is correct");
120 }
121
122 DocSnapshot redoSnapshotState = redoSnapshot.CurrentState();
123 if (redoSnapshotState.Equals(snapshot1State))
124 {
125 WriteLine("snapshot1 and redoSnapshot are equal; redo was successful");
126 }
127 }
128 else
129 {
130 WriteLine("Problem encountered - cannot redo.");
131 }
132 }
133 else
134 {
135 WriteLine("Problem encountered - cannot undo.");
136 }
137
138 }
139 catch (Exception e)
140 {
141 WriteLine(GetExceptionMessage(e));
142 error = true;
143 }
144
145 return error;
146 }
147 }
148}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales