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}
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#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <string>
9#include <iostream>
10#include <PDF/Element.h>
11#include <PDF/ElementBuilder.h>
12#include <PDF/ElementWriter.h>
13#include <PDF/Image.h>
14#include <Common/Matrix2D.h>
15#include "../../LicenseKey/CPP/LicenseKey.h"
16
17using namespace std;
18using namespace pdftron;
19using namespace PDF;
20using namespace SDF;
21using namespace Common;
22
23//---------------------------------------------------------------------------------------
24// The following sample illustrates how to use the UndoRedo API.
25//---------------------------------------------------------------------------------------
26int main(int argc, char *argv[])
27{
28 int ret = 0;
29 try
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 PDFNet::Initialize(LicenseKey);
35
36 // Relative path to the folder containing test files.
37 string input_path = "../../TestFiles/";
38 string output_path = "../../TestFiles/Output/";
39
40 // Open the PDF document.
41 PDFDoc doc((input_path + "newsletter.pdf").c_str());
42
43 UndoManager undo_manager = doc.GetUndoManager();
44
45 // Take a snapshot to which we can undo after making changes.
46 ResultSnapshot snap0 = undo_manager.TakeSnapshot();
47
48 DocSnapshot snap0_state = snap0.CurrentState();
49
50 Page page = doc.PageCreate(); // Start a new page
51
52 ElementBuilder bld; // Used to build new Element objects
53 ElementWriter writer; // Used to write Elements to the page
54 writer.Begin(page); // Begin writing to this page
55
56 // ----------------------------------------------------------
57 // Add JPEG image to the file
58 PDF::Image img = PDF::Image::Create(doc, (input_path + "peppers.jpg").c_str());
59 Element element = bld.CreateImage(img, 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 puts("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").c_str(), SDFDoc::e_incremental, 0);
76
77 if (undo_manager.CanUndo())
78 {
79 ResultSnapshot undo_snap = undo_manager.Undo();
80
81 doc.Save((output_path + "addimage_undone.pdf").c_str(), SDFDoc::e_incremental, 0);
82
83 DocSnapshot undo_snap_state = undo_snap.CurrentState();
84
85 if (undo_snap_state.Equals(snap0_state))
86 {
87 puts("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").c_str(), SDFDoc::e_incremental, 0);
95
96 if (redo_snap.PreviousState().Equals(undo_snap_state))
97 {
98 puts("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 puts("Snap1 and redo_snap are equal; redo was successful");
106 }
107 }
108 else
109 {
110 puts("Problem encountered - cannot redo.");
111 ret = 1;
112 }
113 }
114 else
115 {
116 puts("Problem encountered - cannot undo.");
117 ret = 1;
118 }
119 }
120 catch(Common::Exception& e)
121 {
122 cout << e << endl;
123 ret = 1;
124 }
125 catch (...)
126 {
127 cout << "Unknown Exception" << endl;
128 ret = 1;
129 }
130
131 PDFNet::Terminate();
132
133 return ret;
134}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 . "pdftron"
10)
11
12import "pdftron/Samples/LicenseKey/GO"
13
14// Relative path to the folder containing test files.
15var inputPath = "../../TestFiles/"
16var outputPath = "../../TestFiles/Output/"
17
18//---------------------------------------------------------------------------------------
19// The following sample illustrates how to use the UndoRedo API.
20//---------------------------------------------------------------------------------------
21
22func main(){
23 // The first step in every application using PDFNet is to initialize the
24 // library and set the path to common PDF resources. The library is usually
25 // initialized only once, but calling Initialize() multiple times is also fine.
26 PDFNetInitialize(PDFTronLicense.Key)
27
28 // Open the PDF document.
29 doc := NewPDFDoc(inputPath + "newsletter.pdf")
30
31 undoManager := doc.GetUndoManager()
32
33 // Take a snapshot to which we can undo after making changes.
34 snap0 := undoManager.TakeSnapshot()
35
36 snap0State := snap0.CurrentState()
37
38 // Start a new page
39 page := doc.PageCreate()
40
41 bld := NewElementBuilder() // Used to build new Element objects
42 writer := NewElementWriter() // Used to write Elements to the page
43 writer.Begin(page) // Begin writing to this page
44
45 // ----------------------------------------------------------
46 // Add JPEG image to the file
47 img := ImageCreate(doc.GetSDFDoc(), inputPath + "peppers.jpg")
48
49 element := bld.CreateImage(img, NewMatrix2D(200.0, 0.0, 0.0, 250.0, 50.0, 500.0))
50 writer.WritePlacedElement(element)
51
52 // Finish writing to the page
53 writer.End()
54 doc.PagePushFront(page)
55
56 // Take a snapshot after making changes, so that we can redo later (after undoing first).
57 snap1 := undoManager.TakeSnapshot()
58
59 if snap1.PreviousState().Equals(snap0State){
60 fmt.Println("snap1 previous state equals snap0State; previous state is correct")
61 }
62 snap1State := snap1.CurrentState()
63
64 doc.Save(outputPath + "addimage.pdf", uint(SDFDocE_incremental))
65
66 if undoManager.CanUndo(){
67 undoSnap := undoManager.Undo()
68
69 doc.Save(outputPath + "addimage_undone.pdf", uint(SDFDocE_incremental))
70
71 undoSnapState := undoSnap.CurrentState()
72
73 if undoSnapState.Equals(snap0State){
74 fmt.Println("undoSnapState equals snap0State; undo was successful")
75 }
76
77 if undoManager.CanRedo(){
78 redoSnap := undoManager.Redo()
79
80 doc.Save(outputPath + "addimage_redone.pdf", uint(SDFDocE_incremental))
81
82 if redoSnap.PreviousState().Equals(undoSnapState){
83 fmt.Println("redoSnap previous state equals undoSnapState; previous state is correct")
84 }
85
86 redoSnapState := redoSnap.CurrentState()
87
88 if redoSnapState.Equals(snap1State){
89 fmt.Println("Snap1 and redoSnap are equal; redo was successful")
90 }
91 }else{
92 fmt.Println("Problem encountered - cannot redo.")
93 }
94 }else{
95 fmt.Println("Problem encountered - cannot undo.")
96 }
97 PDFNetTerminate()
98}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.Obj;
8import com.pdftron.common.Matrix2D;
9import com.pdftron.common.PDFNetException;
10import com.pdftron.sdf.UndoManager;
11import com.pdftron.sdf.ResultSnapshot;
12import com.pdftron.sdf.DocSnapshot;
13import com.pdftron.sdf.SDFDoc;
14
15import java.io.File;
16import java.io.IOException;
17import java.io.FileNotFoundException;
18
19//---------------------------------------------------------------------------------------
20// The following sample illustrates how to use the UndoRedo API.
21//---------------------------------------------------------------------------------------
22public class UndoRedoTest
23{
24 public static void main(String[] args)
25 {
26 try
27 {
28 // The first step in every application using PDFNet is to initialize the
29 // library and set the path to common PDF resources. The library is usually
30 // initialized only once, but calling Initialize() multiple times is also fine.
31 PDFNet.initialize(PDFTronLicense.Key());
32
33 // Relative path to the folder containing test files.
34 String input_path = "../../TestFiles/";
35 String output_path = "../../TestFiles/Output/";
36
37 // Open the PDF document.
38 try (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf")) {
39
40 UndoManager undo_manager = doc.getUndoManager();
41
42 // Take a snapshot to which we can undo after making changes.
43 ResultSnapshot snap0 = undo_manager.takeSnapshot();
44
45 DocSnapshot snap0_state = snap0.currentState();
46
47 Page page = doc.pageCreate(); // Start a new page
48
49 ElementBuilder bld = new ElementBuilder(); // Used to build new Element objects
50 ElementWriter writer = new ElementWriter(); // Used to write Elements to the page
51 writer.begin(page); // Begin writing to this page
52
53 // ----------------------------------------------------------
54 // Add JPEG image to the file
55 Image img = Image.create(doc, input_path + "peppers.jpg");
56 Element element = bld.createImage(img, new Matrix2D(200, 0, 0, 250, 50, 500));
57 writer.writePlacedElement(element);
58
59 writer.end(); // Finish writing to the page
60 doc.pagePushFront(page);
61
62 // Take a snapshot after making changes, so that we can redo later (after undoing first).
63 ResultSnapshot snap1 = undo_manager.takeSnapshot();
64
65 if (snap1.previousState().equals(snap0_state))
66 {
67 System.out.println("snap1 previous state equals snap0_state; previous state is correct");
68 }
69
70 DocSnapshot snap1_state = snap1.currentState();
71
72 doc.save(output_path + "addimage.pdf", SDFDoc.SaveMode.INCREMENTAL, null);
73
74 if (undo_manager.canUndo())
75 {
76 ResultSnapshot undo_snap;
77 undo_snap = undo_manager.undo();
78
79 doc.save(output_path + "addimage_undone.pdf", SDFDoc.SaveMode.INCREMENTAL, null);
80
81 DocSnapshot undo_snap_state = undo_snap.currentState();
82
83 if (undo_snap_state.equals(snap0_state))
84 {
85 System.out.println("undo_snap_state equals snap0_state; undo was successful");
86 }
87
88 if (undo_manager.canRedo())
89 {
90 ResultSnapshot redo_snap = undo_manager.redo();
91
92 doc.save(output_path + "addimage_redone.pdf", SDFDoc.SaveMode.INCREMENTAL, null);
93
94 if (redo_snap.previousState().equals(undo_snap_state))
95 {
96 System.out.println("redo_snap previous state equals undo_snap_state; previous state is correct");
97 }
98
99 DocSnapshot redo_snap_state = redo_snap.currentState();
100
101 if (redo_snap_state.equals(snap1_state))
102 {
103 System.out.println("Snap1 and redo_snap are equal; redo was successful");
104 }
105 }
106 else
107 {
108 System.out.println("Problem encountered - cannot redo.");
109 }
110 }
111 else
112 {
113 System.out.println("Problem encountered - cannot undo.");
114 }
115 }
116
117 // Calling Terminate when PDFNet is no longer in use is a good practice, but
118 // is not required.
119 PDFNet.terminate();
120 }
121 catch (Exception e)
122 {
123 e.printStackTrace();
124 }
125 }
126}
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//---------------------------------------------------------------------------------------
7// The following sample illustrates how to use the UndoRedo API.
8//---------------------------------------------------------------------------------------
9const { PDFNet } = require('@pdftron/pdfnet-node');
10const PDFTronLicense = require('../LicenseKey/LicenseKey');
11
12((exports) => {
13
14 exports.runUndoRedoTest = () => {
15
16 const main = async () => {
17 try {
18 // Relative path to the folder containing test files.
19 const inputPath = '../TestFiles/';
20 const outputPath = inputPath + 'Output/';
21
22 // Open the PDF document.
23 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
24
25 const undo_manager = await doc.getUndoManager();
26
27 // Take a snapshot to which we can undo after making changes.
28 const snap0 = await undo_manager.takeSnapshot();
29
30 const snap0_state = await snap0.currentState();
31
32 const page = await doc.pageCreate(); // Start a new page
33
34 const bld = await PDFNet.ElementBuilder.create(); // Used to build new Element objects
35 const writer = await PDFNet.ElementWriter.create(); // Used to write Elements to the page
36 writer.beginOnPage(page); // Begin writing to this page
37
38 // ----------------------------------------------------------
39 // Add JPEG image to the file
40 const img = await PDFNet.Image.createFromFile(doc, inputPath + 'peppers.jpg');
41 const element = await bld.createImageFromMatrix(img, await PDFNet.Matrix2D.create(200, 0, 0, 250, 50, 500));
42 writer.writePlacedElement(element);
43
44 await writer.end(); // Finish writing to the page
45 await doc.pagePushFront(page);
46
47 // Take a snapshot after making changes, so that we can redo later (after undoing first).
48 const snap1 = await undo_manager.takeSnapshot();
49
50 if (await (await snap1.previousState()).equals(snap0_state)) {
51 console.log('snap1 previous state equals snap0_state; previous state is correct');
52 }
53
54 const snap1_state = await snap1.currentState();
55
56 await doc.save(outputPath + 'addimage.pdf', PDFNet.SDFDoc.SaveOptions.e_incremental);
57
58 if (await undo_manager.canUndo()) {
59 const undo_snap = await undo_manager.undo();
60
61 await doc.save(outputPath + 'addimage_undone.pdf', PDFNet.SDFDoc.SaveOptions.e_incremental);
62
63 const undo_snap_state = await undo_snap.currentState();
64
65 if (await undo_snap_state.equals(snap0_state)) {
66 console.log('undo_snap_state equals snap0_state; undo was successful');
67 }
68
69 if (await undo_manager.canRedo()) {
70 const redo_snap = await undo_manager.redo();
71
72 await doc.save(outputPath + 'addimage_redone.pdf', PDFNet.SDFDoc.SaveOptions.e_incremental);
73
74 if (await (await redo_snap.previousState()).equals(undo_snap_state)) {
75 console.log('redo_snap previous state equals undo_snap_state; previous state is correct');
76 }
77
78 const redo_snap_state = await redo_snap.currentState();
79
80 if (await redo_snap_state.equals(snap1_state)) {
81 console.log('Snap1 and redo_snap are equal; redo was successful');
82 }
83 }
84 else {
85 console.log('Problem encountered - cannot redo.');
86 }
87 }
88 else {
89 console.log('Problem encountered - cannot undo.');
90 }
91 } catch (err) {
92 console.log(err.stack);
93 }
94 };
95
96 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
97 };
98 exports.runUndoRedoTest();
99})(exports);
100// eslint-disable-next-line spaced-comment
101//# sourceURL=UndoRedoTest.js
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14//---------------------------------------------------------------------------------------
15// The following sample illustrates how to use the UndoRedo API.
16//---------------------------------------------------------------------------------------
17
18 // The first step in every application using PDFNet is to initialize the
19 // library and set the path to common PDF resources. The library is usually
20 // initialized only once, but calling Initialize() multiple times is also fine.
21 PDFNet::Initialize($LicenseKey);
22 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
23
24 // Open the PDF document.
25 $doc = new PDFDoc($input_path."newsletter.pdf");
26
27 $undo_manager = $doc->GetUndoManager();
28
29 // Take a snapshot to which we can undo after making changes.
30 $snap0 = $undo_manager->TakeSnapshot();
31
32 $snap0_state = $snap0->CurrentState();
33
34 // Start a new page
35 $page = $doc->PageCreate();
36
37 $builder = new ElementBuilder(); // Used to build new Element objects
38 $writer = new ElementWriter(); // Used to write Elements to the page
39
40 $page = $doc->PageCreate(); // Start a new page
41 $writer->Begin($page); // Begin writing to this page
42
43 // ----------------------------------------------------------
44 // Add JPEG image to the output file
45 $img = Image::Create($doc->GetSDFDoc(), $input_path."peppers.jpg");
46
47 $element = $builder->CreateImage($img, new Matrix2D(200.0,0.0,0.0,250.0,50.0,500.0));
48 $writer->WritePlacedElement($element);
49
50 // Finish writing to the page
51 $writer->End();
52 $doc->PagePushFront($page);
53
54 // Take a snapshot after making changes, so that we can redo later (after undoing first).
55 $snap1 = $undo_manager->TakeSnapshot();
56
57 if ($snap1->PreviousState()->Equals($snap0_state))
58 {
59 echo(nl2br("snap1 previous state equals snap0_state; previous state is correct\n"));
60 }
61
62 $snap1_state = $snap1->CurrentState();
63
64 $doc->Save($output_path."addimage.pdf", SDFDoc::e_incremental);
65
66 if ($undo_manager->CanUndo())
67 {
68 $undo_snap = $undo_manager->Undo();
69
70 $doc->Save($output_path."addimage_undone.pdf", SDFDoc::e_incremental);
71
72 $undo_snap_state = $undo_snap->CurrentState();
73
74 if ($undo_snap_state->Equals($snap0_state))
75 {
76 echo(nl2br("undo_snap_state equals snap0_state; undo was successful\n"));
77 }
78
79 if ($undo_manager->CanRedo())
80 {
81 $redo_snap = $undo_manager->Redo();
82
83 $doc->Save($output_path."addimage_redone.pdf", SDFDoc::e_incremental);
84
85 if ($redo_snap->PreviousState()->Equals($undo_snap_state))
86 {
87 echo(nl2br("redo_snap previous state equals undo_snap_state; previous state is correct\n"));
88 }
89
90 $redo_snap_state = $redo_snap->CurrentState();
91
92 if ($redo_snap_state->Equals($snap1_state))
93 {
94 echo(nl2br("Snap1 and redo_snap are equal; redo was successful\n"));
95 }
96 }
97 else
98 {
99 echo(nl2br("Problem encountered - cannot redo.\n"));
100 }
101 }
102 else
103 {
104 echo(nl2br("Problem encountered - cannot undo.\n"));
105 }
106 PDFNet::Terminate();
107?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14# Relative path to the folder containing test files.
15input_path = "../../TestFiles/"
16output_path = "../../TestFiles/Output/"
17
18#---------------------------------------------------------------------------------------
19# The following sample illustrates how to use the UndoRedo API.
20#---------------------------------------------------------------------------------------
21
22def main():
23 # The first step in every application using PDFNet is to initialize the
24 # library and set the path to common PDF resources. The library is usually
25 # initialized only once, but calling Initialize() multiple times is also fine.
26 PDFNet.Initialize(LicenseKey)
27
28 # Open the PDF document.
29 doc = PDFDoc(input_path + "newsletter.pdf")
30
31 undo_manager = doc.GetUndoManager()
32
33 # Take a snapshot to which we can undo after making changes.
34 snap0 = undo_manager.TakeSnapshot()
35
36 snap0_state = snap0.CurrentState()
37
38 # Start a new page
39 page = doc.PageCreate()
40
41 bld = ElementBuilder() # Used to build new Element objects
42 writer = ElementWriter() # Used to write Elements to the page
43 writer.Begin(page) # Begin writing to this page
44
45 # ----------------------------------------------------------
46 # Add JPEG image to the file
47 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
48
49 element = bld.CreateImage(img, Matrix2D(200, 0, 0, 250, 50, 500))
50 writer.WritePlacedElement(element)
51
52 # Finish writing to the page
53 writer.End()
54 doc.PagePushFront(page)
55
56 # Take a snapshot after making changes, so that we can redo later (after undoing first).
57 snap1 = undo_manager.TakeSnapshot()
58
59 if snap1.PreviousState().Equals(snap0_state):
60 print("snap1 previous state equals snap0_state; previous state is correct")
61
62 snap1_state = snap1.CurrentState()
63
64 doc.Save(output_path + "addimage.pdf", SDFDoc.e_incremental)
65
66 if undo_manager.CanUndo():
67 undo_snap = undo_manager.Undo()
68
69 doc.Save(output_path + "addimage_undone.pdf", SDFDoc.e_incremental)
70
71 undo_snap_state = undo_snap.CurrentState()
72
73 if undo_snap_state.Equals(snap0_state):
74 print("undo_snap_state equals snap0_state; undo was successful")
75
76 if undo_manager.CanRedo():
77 redo_snap = undo_manager.Redo()
78
79 doc.Save(output_path + "addimage_redone.pdf", SDFDoc.e_incremental)
80
81 if redo_snap.PreviousState().Equals(undo_snap_state):
82 print("redo_snap previous state equals undo_snap_state; previous state is correct")
83
84 redo_snap_state = redo_snap.CurrentState()
85
86 if redo_snap_state.Equals(snap1_state):
87 print("Snap1 and redo_snap are equal; redo was successful")
88 else:
89 print("Problem encountered - cannot redo.")
90 else:
91 print("Problem encountered - cannot undo.")
92 PDFNet.Terminate()
93
94if __name__ == '__main__':
95 main()
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
1'---------------------------------------------------------------------------------------
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3' Consult legal.txt regarding legal and license information.
4'---------------------------------------------------------------------------------------
5Imports System
6Imports pdftron
7Imports pdftron.Common
8Imports pdftron.PDF
9Imports pdftron.SDF
10
11Module UndoRedoTestVB
12 Dim pdfNetLoader As PDFNetLoader
13 Sub New()
14 pdfNetLoader = pdftron.PDFNetLoader.Instance()
15 End Sub
16
17
18 Sub Main()
19 ' The first step in every application using PDFNet is to initialize the
20 ' library and set the path to common PDF resources. The library is usually
21 ' initialized only once, but calling Initialize() multiple times is also fine.
22 PDFNet.Initialize(PDFTronLicense.Key)
23
24 ' Relative path to the folder containing test files.
25 Dim input_path As String = "../../../../TestFiles/"
26 Dim output_path As String = "../../../../TestFiles/Output/"
27
28 Try
29 ' Open the PDF document.
30 Using doc As PDFDoc = New PDFDoc(input_path & "newsletter.pdf")
31 Using bld As ElementBuilder = New ElementBuilder() ' Used to build new Element objects
32 Using writer As ElementWriter = New ElementWriter() ' Used to write Elements to the page
33 Dim undo_manager As UndoManager = doc.GetUndoManager()
34 ' Take a snapshot to which we can undo after making changes.
35 Dim snap0 As ResultSnapshot = undo_manager.TakeSnapshot()
36 Dim snap0_state As DocSnapshot = snap0.CurrentState()
37 Dim page As Page = doc.PageCreate() ' Start a new page
38 writer.Begin(page) ' Begin writing to this page
39 ' ----------------------------------------------------------
40 ' Add JPEG image to the file
41 Dim img As Image = Image.Create(doc, input_path & "peppers.jpg")
42 Dim element As Element = bld.CreateImage(img, New Matrix2D(200, 0, 0, 250, 50, 500))
43 writer.WritePlacedElement(element)
44 writer.End() ' Finish writing to the page
45 doc.PagePushFront(page)
46
47 ' Take a snapshot after making changes, so that we can redo later (after undoing first).
48 Dim snap1 As ResultSnapshot = undo_manager.TakeSnapshot()
49 If snap1.PreviousState().Equals(snap0_state) Then
50 Console.WriteLine("snap1 previous state equals snap0_state; previous state is correct")
51 End If
52
53 Dim snap1_state As DocSnapshot = snap1.CurrentState()
54 doc.Save(output_path & "addimage.pdf", SDFDoc.SaveOptions.e_incremental)
55
56 If undo_manager.CanUndo() Then
57 Dim undo_snap As ResultSnapshot = undo_manager.Undo()
58 doc.Save(output_path & "addimage_undone.pdf", SDFDoc.SaveOptions.e_incremental)
59
60 Dim undo_snap_state As DocSnapshot = undo_snap.CurrentState()
61 If undo_snap_state.Equals(snap0_state) Then
62 Console.WriteLine("undo_snap_state equals snap0_state; undo was successful")
63 End If
64
65 If undo_manager.CanRedo() Then
66 Dim redo_snap As ResultSnapshot = undo_manager.Redo()
67 doc.Save(output_path & "addimage_redone.pdf", SDFDoc.SaveOptions.e_incremental)
68
69 If redo_snap.PreviousState().Equals(undo_snap_state) Then
70 Console.WriteLine("redo_snap previous state equals undo_snap_state; previous state is correct")
71 End If
72
73 Dim redo_snap_state As DocSnapshot = redo_snap.CurrentState()
74
75 If redo_snap_state.Equals(snap1_state) Then
76 Console.WriteLine("Snap1 and redo_snap are equal; redo was successful")
77 End If
78 Else
79 Console.WriteLine("Problem encountered - cannot undo.")
80 End If
81 Else
82 Console.WriteLine("Problem encountered - cannot undo.")
83 End If
84 End Using
85 End Using
86 End Using
87
88 Catch e As PDFNetException
89 Console.WriteLine(e.Message)
90 End Try
91 PDFNet.Terminate()
92 End Sub
93End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales