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 Obj-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 iOS 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
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8#include <math.h>
9
10//---------------------------------------------------------------------------------------
11// The following sample illustrates how to use the UndoRedo API.
12//---------------------------------------------------------------------------------------
13int main(int argc, char *argv[])
14{
15 @autoreleasepool
16 {
17 int ret = 0;
18
19 @try
20 {
21 // The first step in every application using PDFNet is to initialize the
22 // library and set the path to common PDF resources. The library is usually
23 // initialized only once, but calling Initialize() multiple times is also fine.
24 [PTPDFNet Initialize: 0];
25
26 // Open the PDF document.
27 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
28
29 PTSDFUndoManager* undo_manager = [doc GetUndoManager];
30
31 // Take a snapshot to which we can undo after making changes.
32 PTSDFResultSnapshot* snap0 = [undo_manager TakeSnapshot];
33
34 PTSDFDocSnapshot* snap0_state = [snap0 CurrentState];
35
36 PTElementBuilder* builder = [[PTElementBuilder alloc] init]; // Used to build new Element objects
37 PTElementWriter* writer = [[PTElementWriter alloc] init]; // Used to write Elements to the page
38
39 PTPDFRect * rect = [[PTPDFRect alloc] init];
40 [rect Set: 0 y1: 0 x2: 612 y2: 792];
41
42 PTPage* page = [doc PageCreate: rect]; // Start a new page
43
44 [writer WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // Begin writing to this page
45
46 // ----------------------------------------------------------
47 // Add JPEG image to the file
48 PTImage* img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/peppers.jpg"];
49 PTElement* element = [builder CreateImageWithMatrix: img mtx: [[PTMatrix2D alloc] initWithA: 200 b: 0 c: 0 d: 250 h: 50 v: 500]];
50 [writer WritePlacedElement: element];
51
52 [writer End]; // Finish writing to the page
53 [doc PagePushFront: page];
54
55 // Take a snapshot after making changes, so that we can redo later (after undoing first).
56 PTSDFResultSnapshot* snap1 = [undo_manager TakeSnapshot];
57
58 if ([[snap1 PreviousState] Equals: snap0_state])
59 {
60 NSLog(@"snap1 previous state equals snap0_state; previous state is correct");
61 }
62
63 PTSDFDocSnapshot* snap1_state = [snap1 CurrentState];
64
65 [doc SaveToFile: @"../../TestFiles/Output/addimage.pdf" flags: e_ptincremental];
66
67 if ([undo_manager CanUndo])
68 {
69 PTSDFResultSnapshot* undo_snap = [undo_manager Undo];
70
71 [doc SaveToFile: @"../../TestFiles/Output/addimage_undone.pdf" flags: e_ptincremental];
72
73 PTSDFDocSnapshot* undo_snap_state = [undo_snap CurrentState];
74
75 if ([undo_snap_state Equals: snap0_state])
76 {
77 NSLog(@"undo_snap_state equals snap0_state; undo was successful");
78 }
79
80 if ([undo_manager CanRedo])
81 {
82 PTSDFResultSnapshot* redo_snap = [undo_manager Redo];
83
84 [doc SaveToFile: @"../../TestFiles/Output/addimage_redone.pdf" flags: e_ptincremental];
85
86 if ([[redo_snap PreviousState] Equals: undo_snap_state])
87 {
88 NSLog(@"redo_snap previous state equals undo_snap_state; previous state is correct");
89 }
90
91 PTSDFDocSnapshot* redo_snap_state = [redo_snap CurrentState];
92
93 if ([redo_snap_state Equals: snap1_state])
94 {
95 NSLog(@"Snap1 and redo_snap are equal; redo was successful");
96 }
97 }
98 else
99 {
100 NSLog(@"Problem encountered - cannot redo.");
101 ret = 1;
102 }
103 }
104 else
105 {
106 NSLog(@"Problem encountered - cannot undo.");
107 ret = 1;
108 }
109 }
110 @catch(NSException *e)
111 {
112 NSLog(@"%@", e.reason);
113 ret = 1;
114 }
115 [PTPDFNet Terminate: 0];
116 return ret;
117 }
118}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales