showing results for - "pdf javascript search text"
George
17 Sep 2019
1var searchText = "JavaScript";
2function searchPage(doc, pageNumber) {
3  return doc.getPage(pageNumber).then(function (page) {
4    return page.getTextContent();
5  }).then(function (content) {
6    // Search combined text content using regular expression
7    var text = content.items.map(function (i) { return i.str; }).join('');
8    var re = new RegExp("(.{0,20})" + searchText + "(.{0,20})", "gi"), m;
9    var lines = [];
10    while (m = re.exec(text)) {
11      var line = (m[1] ? "..." : "") + m[0] + (m[2] ? "..." : "");
12      lines.push(line);
13    }
14    return {page: pageNumber, items: lines};
15  });
16}
17
18var loading = PDFJS.getDocument("//cdn.mozilla.net/pdfjs/tracemonkey.pdf");
19loading.promise.then(function (doc) {
20  var results = [];
21  for (var i = 1; i <= doc.numPages; i++)
22    results.push(searchPage(doc, i));
23  return Promise.all(results);
24}).then(function (searchResults) {
25  // Display results using divs
26  searchResults.forEach(function (result) {
27    var div = document.createElement('div'); div.className="pr"; document.body.appendChild(div);
28    div.textContent = 'Page ' + result.page + ':';
29    result.items.forEach(function (s) {
30      var div2 = document.createElement('div'); div2.className="prl"; div.appendChild(div2);
31      div2.textContent = s; 
32    });
33  });
34}).catch(console.error);