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 Xamarin SDK.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6using System;
7using pdftron;
8using pdftron.Common;
9using pdftron.PDF;
10using pdftron.SDF;
11
12using NUnit.Framework;
13
14namespace MiscellaneousSamples
15{
16 /// <summary>
17 //---------------------------------------------------------------------------------------
18 // The following sample illustrates how to use the UndoRedo API.
19 //---------------------------------------------------------------------------------------
20 /// </summary>
21 [TestFixture]
22 public class UndoRedoTest
23 {
24
25 /// <summary>
26 /// The main entry point for the application.
27 /// </summary>
28 [Test]
29 public static void Sample()
30 {
31 // The first step in every application using PDFNet is to initialize the
32 // library and set the path to common PDF resources. The library is usually
33 // initialized only once, but calling Initialize() multiple times is also fine.
34
35 // Relative path to the folder containing test files.
36 const string input_path = "TestFiles/";
37
38 try
39 {
40 // Open the PDF document.
41 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "newsletter.pdf")))
42 using (ElementBuilder bld = new ElementBuilder()) // Used to build new Element objects
43 using (ElementWriter writer = new ElementWriter()) // Used to write Elements to the page
44 {
45 UndoManager undo_manager = doc.GetUndoManager();
46
47 // Take a snapshot to which we can undo after making changes.
48 ResultSnapshot snap0 = undo_manager.TakeSnapshot();
49
50 DocSnapshot snap0_state = snap0.CurrentState();
51
52 Page page = doc.PageCreate(); // Start a new page
53
54 writer.Begin(page); // Begin writing to this page
55
56 // ----------------------------------------------------------
57 // Add JPEG image to the file
58 Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
59 Element element = bld.CreateImage(img, new Matrix2D(200, 0, 0, 250, 50, 500));
60 writer.WritePlacedElement(element);
61
62 writer.End(); // Finish writing to the page
63 doc.PagePushFront(page);
64
65 // Take a snapshot after making changes, so that we can redo later (after undoing first).
66 ResultSnapshot snap1 = undo_manager.TakeSnapshot();
67
68 if (snap1.PreviousState().Equals(snap0_state))
69 {
70 Console.WriteLine("snap1 previous state equals snap0_state; previous state is correct");
71 }
72
73 DocSnapshot snap1_state = snap1.CurrentState();
74
75 doc.Save(Utils.CreateExternalFile("addimage.pdf"), SDFDoc.SaveOptions.e_incremental);
76
77 if (undo_manager.CanUndo())
78 {
79 ResultSnapshot undo_snap = undo_manager.Undo();
80
81 doc.Save(Utils.CreateExternalFile("addimage_undone.pdf"), SDFDoc.SaveOptions.e_incremental);
82
83 DocSnapshot undo_snap_state = undo_snap.CurrentState();
84
85 if (undo_snap_state.Equals(snap0_state))
86 {
87 Console.WriteLine("undo_snap_state equals snap0_state; undo was successful");
88 }
89
90 if (undo_manager.CanRedo())
91 {
92 ResultSnapshot redo_snap = undo_manager.Redo();
93
94 doc.Save(Utils.CreateExternalFile("addimage_redone.pdf"), SDFDoc.SaveOptions.e_incremental);
95
96 if (redo_snap.PreviousState().Equals(undo_snap_state))
97 {
98 Console.WriteLine("redo_snap previous state equals undo_snap_state; previous state is correct");
99 }
100
101 DocSnapshot redo_snap_state = redo_snap.CurrentState();
102
103 if (redo_snap_state.Equals(snap1_state))
104 {
105 Console.WriteLine("Snap1 and redo_snap are equal; redo was successful");
106 }
107 }
108 else
109 {
110 Console.WriteLine("Problem encountered - cannot redo.");
111 }
112 }
113 else
114 {
115 Console.WriteLine("Problem encountered - cannot undo.");
116 }
117 }
118 }
119 catch (PDFNetException e)
120 {
121 Console.WriteLine(e.Message);
122 Assert.True(false);
123 }
124 }
125 }
126}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales