UndoRedoTest

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 Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software 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
12namespace UndoRedoTestCS
13{
14 /// <summary>
15 //---------------------------------------------------------------------------------------
16 // The following sample illustrates how to use the UndoRedo API.
17 //---------------------------------------------------------------------------------------
18 /// </summary>
19 class Class1
20 {
21 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
22 static Class1() {}
23
24 /// <summary>
25 /// The main entry point for the application.
26 /// </summary>
27 static void Main(string[] args)
28 {
29 // The first step in every application using PDFNet is to initialize the
30 // library and set the path to common PDF resources. The library is usually
31 // initialized only once, but calling Initialize() multiple times is also fine.
32 PDFNet.Initialize(PDFTronLicense.Key);
33
34 // Relative path to the folder containing test files.
35 string input_path = "../../../../TestFiles/";
36 string output_path = "../../../../TestFiles/Output/";
37
38 try
39 {
40 // Open the PDF document.
41 using (PDFDoc doc = new PDFDoc(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, 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(output_path + "addimage.pdf", SDFDoc.SaveOptions.e_incremental);
76
77 if (undo_manager.CanUndo())
78 {
79 ResultSnapshot undo_snap = undo_manager.Undo();
80
81 doc.Save(output_path + "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(output_path + "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 }
123 PDFNet.Terminate();
124 }
125 }
126}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales