Find and Replace Text

This example showcases the Find and Replace functionality, allowing the user to replace specific terms or phrases in a document. The dialog is pre-filled with a search term and replacement text, enabling quick interaction and demonstration of the feature.

// Viewer instance: Represents the PDF viewer instance used for document rendering and interaction. var viewer; // Execute code when the window has fully loaded: window.onload = async function () { // Initialize the PDF viewer with configuration options // Uncomment and set your license key if required // DsPdfViewer.LicenseKey = "***key***"; viewer = new DsPdfViewer('#viewer', { restoreViewStateOnLoad: false, supportApi: getSupportApiSettings() }); viewer.addDefaultPanels(); // Configure toolbar buttons for different layouts (default, mobile, fullscreen) viewer.toolbarLayout.viewer = { default: ["save", "$navigation", "$split", "$zoom", "edit-undo", "edit-redo", "$split", 'doc-title', "about"], mobile: ["save", "$navigation", "$split", "$zoom", "edit-undo", "edit-redo", "$split", 'doc-title', "about"], fullscreen: ["$fullscreen", "save", "$navigation", "$split", "$zoom", "edit-undo", "edit-redo", "$split", 'doc-title', "about"] }; // Apply the toolbar layout configuration viewer.applyToolbarLayout(); // Path to the PDF document to be opened var pdf = "/document-solutions/javascript-pdf-viewer/demos/product-bundles/assets/pdf/find-and-replace.pdf"; // Open the document and set the zoom mode to 1 (Fit Width) await viewer.open(pdf); viewer.zoom = { mode: 1 }; // Configure options for text search var findOptions = { Text: 'wetlands', // Text to search for MatchCase: false, // Case insensitive search WholeWord: true // Match whole words only }; // Use the floating search bar to perform a find-and-replace operation viewer.plugin.floatingSearchBar.then(f => { f.setOptions(findOptions); // Apply search options f.show(); // Show the floating search bar f.replaceMode = true; // Enable replace mode f.replaceWith = "WETLANDS"; // Set the replacement text f.onEnterKey(); // Execute the find action }); };
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Find and Replace Text</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="./src/styles.css"> <script src="/document-solutions/javascript-pdf-viewer/demos/product-bundles/build/dspdfviewer.js"></script> <script src="/document-solutions/javascript-pdf-viewer/demos/product-bundles/build/wasmSupportApi.js"></script> <script src="/document-solutions/javascript-pdf-viewer/demos/resource/js/init.js"></script> <script src="./src/app.js"></script> </head> <body> <div id="viewer"></div> </body> </html>
#viewer { height: 100%; }