Some test text!

Discord Logo

Chat with us

PDFTron is now Apryse, learn more here.

Undo / redo edits to a PDF file using JavaScript

More languages

More languages
Java (Android)
C# (.NET Core)
JS (Node.js)
C# (UWP)
C# (Xamarin)

The PDFTron 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 JavaScript code shows how to use PDFTron 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 JavaScript PDF Library.

Get Started Samples Download

To run this sample, get started with a free trial of Apryse SDK.

// Copyright (c) 2001-2023 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.

// The following sample illustrates how to use the UndoRedo API.
const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {

	exports.runUndoRedoTest = () => {

		const main = async () => {
			try {
				// Relative path to the folder containing test files.
				const inputPath = '../TestFiles/';
				const outputPath = inputPath + 'Output/';

				// Open the PDF document.
				const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');

				const undo_manager = await doc.getUndoManager();

				// Take a snapshot to which we can undo after making changes.
				const snap0 = await undo_manager.takeSnapshot();

				const snap0_state = await snap0.currentState();

				const page = await doc.pageCreate();	// Start a new page

				const bld = await PDFNet.ElementBuilder.create();		// Used to build new Element objects
				const writer = await PDFNet.ElementWriter.create();	// Used to write Elements to the page	
				writer.beginOnPage(page);		// Begin writing to this page

				// ----------------------------------------------------------
				// Add JPEG image to the file
				const img = await PDFNet.Image.createFromFile(doc, inputPath + 'peppers.jpg');
				const element = await bld.createImageFromMatrix(img, await PDFNet.Matrix2D.create(200, 0, 0, 250, 50, 500));

				await writer.end();	// Finish writing to the page
				await doc.pagePushFront(page);

				// Take a snapshot after making changes, so that we can redo later (after undoing first).
				const snap1 = await undo_manager.takeSnapshot();

				if (await (await snap1.previousState()).equals(snap0_state)) {
					console.log('snap1 previous state equals snap0_state; previous state is correct');

				const snap1_state = await snap1.currentState();

				await + 'addimage.pdf', PDFNet.SDFDoc.SaveOptions.e_incremental);

				if (await undo_manager.canUndo()) {
					const undo_snap = await undo_manager.undo();

					await + 'addimage_undone.pdf', PDFNet.SDFDoc.SaveOptions.e_incremental);

					const undo_snap_state = await undo_snap.currentState();

					if (await undo_snap_state.equals(snap0_state)) {
						console.log('undo_snap_state equals snap0_state; undo was successful');

					if (await undo_manager.canRedo()) {
						const redo_snap = await undo_manager.redo();

						await + 'addimage_redone.pdf', PDFNet.SDFDoc.SaveOptions.e_incremental);

						if (await (await redo_snap.previousState()).equals(undo_snap_state)) {
							console.log('redo_snap previous state equals undo_snap_state; previous state is correct');

						const redo_snap_state = await redo_snap.currentState();

						if (await redo_snap_state.equals(snap1_state)) {
							console.log('Snap1 and redo_snap are equal; redo was successful');
					else {
						console.log('Problem encountered - cannot redo.');
				else {
					console.log('Problem encountered - cannot undo.');
			} catch (err) {

		PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
// eslint-disable-next-line spaced-comment
//# sourceURL=UndoRedoTest.js