Some test text!

menu
Advanced text searchkeyboard_arrow_down

Advanced text search

Besides the basic text search functions from WebViewer UI, WebViewer provides methods for more low level control of text search. They are:

The textSearchInit function is used to start a text search and the displaySearchResult, displayAdditionalSearchResult, and setActiveSearchResult functions are used to highlight the results. An example of textSearchInit being used can be seen below:

WebViewer({ ... }, viewerElement).then(instance => {
  const { docViewer, Annotations } = instance;
  const ResultCode = viewerElement.querySelector('iframe').contentWindow.XODText.ResultCode;

  docViewer.setSearchHighlightColors({
    // setSearchHighlightColors accepts both Annotations.Color objects or 'rgba' strings
    searchResult: new Annotations.Color(0, 0, 255, 0.5),
    activeSearchResult: 'rgba(0, 255, 0, 0.5)'
  });

  docViewer.on('documentLoaded', () => {
    const searchText = 'TEXT TO SEARCH';
    const mode = docViewer.SearchMode.e_page_stop | docViewer.SearchMode.e_highlight;
    // full search finds all instances of the search term while non full search returns a single 'result'
    const isFullSearch = true;

    docViewer.textSearchInit(searchText, mode, isFullSearch, results => {
      // with 'e_page_stop' mode, the callback is invoked after each page has been searched.
      if (results.resultCode === ResultCode.e_found) {
        const textQuad = results.quads[0].getPoints(); // getPoints will return Quad objects
        // now that we have the result Quads, it's possible to highlight text or create annotations on top of the text
      }
    });
  });
});

In the above code the textSearchInit method is used to search the document. It takes the following input parameters:

  • searchPattern: A pattern to search for
  • mode: A number that encodes the search options, generated by bitwise ORing options together
  • fullSearch: A boolean value for whether to search the whole document or not
  • searchCallBack: A callback function that get called when a match has been found or at the end of document (or when using e_page_stop, at the end of a page)

The 'mode' value it takes in can be created by doing bitwise OR operations on the different SearchMode properties. The search modes are:

  • e_case_sensitive: Text must match the case of the search term
  • e_search_up: Search starts on the last page, and search backwards to first page
  • e_page_stop: Search will invoke the callback function when it finishes searching a page
  • e_highlight: Bounding box of found term will be included
  • e_ambient_string: Characters surrounding the search term will be included
  • e_whole_word: Text must be a whole word

When the callback function is called, it receives a SearchResults object that has a few useful properties. It has a 'resultCode' property, which has one of the following values

  • XODText.ResultCode.e_page: Reached the end of a page
  • XODText.ResultCode.e_found: Found a match
  • XODText.ResultCode.e_done: Done searching the document

The searchResults will also have a 'quads' property that contains an array of textQuad objects. You can call the getPoints function on textQuad objects to receive a Quad object.

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close