Undo/Redo PDF Edits - PHP 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<?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?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales