// 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); // Set search options
f.show(); // Display the floating search bar
f.setState({ // Configure replace options
replaceWith: "WETLANDS",
replaceMode: true
});
f.onEnterKey(); // Trigger the find action
});
/**
* Event listener for the "add-replace-highlights" event.
* Deduplicates replace highlights for all pages.
*/
viewer.eventBus.on("add-replace-highlights", function (args) {
const allReplaceHighlights = viewer.highlightManager.replaceHighlights;
// Iterate through each page and deduplicate highlights
for (const pageIndex in allReplaceHighlights) {
const replaceHighlights = allReplaceHighlights[pageIndex];
deduplicateReplaceHighlights(replaceHighlights);
}
// Notify the viewer about state changes after deduplication
viewer.plugin.raiseStateChanged();
});
};
/**
* Removes duplicate highlights from the given array.
* Duplicates are determined by comparing `rects`, an array of arrays.
* Each inner array is rounded before comparison to ensure numerical consistency.
*
* @param {Array} replaceHighlights - Array of highlights to deduplicate.
*/
function deduplicateReplaceHighlights(replaceHighlights) {
for (let i = replaceHighlights.length - 1; i >= 0; i--) {
const h = replaceHighlights[i];
for (let j = i - 1; j >= 0; j--) {
const other = replaceHighlights[j];
// Check if `rects` arrays are identical (considering rounding and nested arrays)
if (
h.rects.length === other.rects.length &&
h.rects.every((rect, rectIndex) =>
rect.every((value, index) => Math.round(value) === Math.round(other.rects[rectIndex][index]))
)
) {
// Remove duplicate highlight
replaceHighlights.splice(i, 1);
break;
}
}
}
}
<!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%;
}