Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

pdf javascript search text

var searchText = "JavaScript";
function searchPage(doc, pageNumber) {
  return doc.getPage(pageNumber).then(function (page) {
    return page.getTextContent();
  }).then(function (content) {
    // Search combined text content using regular expression
    var text = content.items.map(function (i) { return i.str; }).join('');
    var re = new RegExp("(.{0,20})" + searchText + "(.{0,20})", "gi"), m;
    var lines = [];
    while (m = re.exec(text)) {
      var line = (m[1] ? "..." : "") + m[0] + (m[2] ? "..." : "");
      lines.push(line);
    }
    return {page: pageNumber, items: lines};
  });
}

var loading = PDFJS.getDocument("//cdn.mozilla.net/pdfjs/tracemonkey.pdf");
loading.promise.then(function (doc) {
  var results = [];
  for (var i = 1; i <= doc.numPages; i++)
    results.push(searchPage(doc, i));
  return Promise.all(results);
}).then(function (searchResults) {
  // Display results using divs
  searchResults.forEach(function (result) {
    var div = document.createElement('div'); div.className="pr"; document.body.appendChild(div);
    div.textContent = 'Page ' + result.page + ':';
    result.items.forEach(function (s) {
      var div2 = document.createElement('div'); div2.className="prl"; div.appendChild(div2);
      div2.textContent = s; 
    });
  });
}).catch(console.error);
Comment

PREVIOUS NEXT
Code Example
Javascript :: operador ternario angular click 
Javascript :: reqeuest body in hapijs 
Javascript :: useEffectOnce 
Javascript :: res : [ Circular ] nodejs 
Javascript :: show image in popup from owl carousel pop up 
Javascript :: Angular watching for changes in $http.pendingRequests from directive 
Javascript :: fluentmigrator update row where 
Javascript :: how to english paragraph matching in javascript 
Javascript :: what was the reaction of others bostonh tea party 
Javascript :: javascript 2 decimal float array elements 
Javascript :: javascript factor chain 
Javascript :: react function called last state 
Javascript :: conditional rendering alert if input fields are empty 
Javascript :: bind jquery trough name 
Javascript :: data-item-id 
Javascript :: angular add debounce time before putting valu in subject next 
Javascript :: pdfjs add custom event handler to viewer.js 
Javascript :: zeamster examples react node 
Javascript :: react generate ethereum address 
Javascript :: tailwindcss with django 
Javascript :: image image using next and previous button in javascript 
Javascript :: use vue cdn with firestore 
Javascript :: react router tutorial medium 
Javascript :: defoult function atributes 
Javascript :: js extract all tags not have attr 
Javascript :: appendchild element once if element presense in js 
Javascript :: hark javascript 
Javascript :: javascript and python graphs for data analysis 
Javascript :: Ocultar o mostrar elementos HTML con JQuery 
Javascript :: how to add functionality inside js object 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =