window.onload = function () {
//DsPdfViewer.LicenseKey = "***key***";
var viewer = new DsPdfViewer('#viewer', { restoreViewStateOnLoad: false, language: "ja" });
viewer.addDefaultPanels();
var pdf = "/diodocs/pdfviewer/demos/product-bundles/assets/pdf/diodocs_a4_full.pdf";
loadPdf(viewer, pdf, "DioDocs")
}
// PDFを開き、指定したテキストをプログラムで検索し、すべてハイライト表示します。
async function loadPdf(viewer, pdf, searchText) {
var afterOpenPromise = new Promise((resolve) => { viewer.onAfterOpen.register(() => { resolve(); }); });
await viewer.open(pdf);
await afterOpenPromise;
// 必要に応じて検索条件を調整します。
var findOptions = {
Text: searchText,
MatchCase: true,
WholeWord: true,
StartsWith: false,
EndsWith: false,
Wildcards: false,
Proximity: false,
SearchBackward: false,
HighlightAll: true
};
var searchIterator = await viewer.searcher.search(findOptions);
var searchResult = await searchIterator.next();
viewer.searcher.cancel();
viewer.searcher.highlight(searchResult.value);
}
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>コードによるテキスト検索</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./src/styles.css">
<script src="/diodocs/pdfviewer/demos/product-bundles/build/dspdfviewer.js"></script>
<script src="/diodocs/pdfviewer/demos/product-bundles/build/wasmSupportApi.js"></script>
<script src="/diodocs/pdfviewer/demos/resource/js/init.js"></script>
<script src="./src/app.js"></script>
</head>
<body>
<div id="viewer"></div>
</body>
</html>
#viewer {
height: 100%;
}