Undo/Redo PDF Edits - Go Sample Code

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 code (provided in Python, C++, C#, Java, Node.js, PHP, Ruby, Go and VB) 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-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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales