> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/search/text-search-full-api.md).

# Programmatic search without WebViewer

Enhance your search functionality with full API capabilities, enabling document loading and content search without WebViewer rendering. Boost performance by leveraging the TextSearch class to efficien

## Using full API search

It's possible to load a document and search through its content without rendering the document in WebViewer. This can be helpful for performance as the document doesn't need to be rendered to search through it. This feature is provided by the [`TextSearch`](https://sdk.apryse.com/api/web/Core.PDFNet.TextSearch.html) class and an example can be found below.

{% hint style="info" %}
This guide uses the [full API without a viewer.](/web/full-api/running-without-viewer.md)
{% endhint %}

<pre class="language-js" data-line-numbers><code class="lang-js">const main = async () => {
  try {
    const doc = await PDFNet.PDFDoc.createFromURL('PATH TO File/fileName.pdf');
    doc.initSecurityHandler();
    doc.lock();

    const txtSearch = await PDFNet.TextSearch.create();
    let searchMode = PDFNet.TextSearch.Mode;
    let mode = PDFNet.TextSearch.Mode.e_whole_word | PDFNet.TextSearch.Mode.e_highlight;
    // 'pattern' can be a regular express when using 'e_reg_expression' mode
    let pattern = 'string to search';

    txtSearch.begin(doc, pattern, mode);
    let result = await txtSearch.run();

    while (true) {
      if (result.code === PDFNet.TextSearch.ResultCode.e_found) {
        let highlights = result.highlights;
        highlights.begin(doc);

        while (await highlights.hasNext()) {
          // 'highlights' will have multiple Quad objects if 'pattern' is on multiple lines
          let quad = await highlights.getCurrentQuads();
          await highlights.next();
        }
      } else if (result.code === PDFNet.TextSearch.ResultCode.e_page) {
        console.log(`Finish searching page ${result.page_num}`);
        // will only get 'result' for end of page if 'PDFNet.TextSearch.Mode.e_page_stop' was added to 'mode'
      } else if (result.code === PDFNet.TextSearch.ResultCode.e_done) {
        console.log(`Finish searching the document`);
        // if 'run()' is called again, it'll return the same 'result' oject with 'result.code' of 'e_done'
        break;
      }

      // It's possible to change the search pattern or mode while searching
      // However any text or pages searched will not be searched again
      // txtSearch.setMode(mode);
      // txtSearch.setPattern('new string to search');

      result = await txtSearch.run();
    }
  } catch (err) {
    console.log(err);
  }
};

PDFNet.runWithCleanup(main, '<code class="expression">visitor.claims.wvKey || "YOUR_LICENSE_KEY"</code>');
</code></pre>

[PDFNet.PDFDoc.createFromUrl](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#.createFromURL__anchor) [PDFNet.PDFDoc.initSecurityHandler](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#initSecurityHandler__anchor) [PDFNet.PDFDoc.lock](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#lock__anchor) [PDFNet.TextSearch.create](https://sdk.apryse.com/api/web/Core.PDFNet.TextSearch.html#.create__anchor) [PDFNet.TextSearch.begin](https://sdk.apryse.com/api/web/Core.PDFNet.TextSearch.html#begin__anchor) [PDFNet.TextSearch.run](https://sdk.apryse.com/api/web/Core.PDFNet.TextSearch.html#run__anchor)

Like other PDFNet code, start by using [runWithCleanup](https://sdk.apryse.com/api/web/Core.PDFNet.html#.runWithCleanup__anchor) to run the code. Afterwards, create new [`TextSearch`](https://sdk.apryse.com/api/web/Core.PDFNet.TextSearch.html) and [`PDFDoc`](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html) objects (in the above sample, we used [createFromURL](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#.createFromURL__anchor) but other methods work as well). To start the search, call the [begin](https://sdk.apryse.com/api/web/Core.PDFNet.TextSearch.html#begin__anchor) method on the `TextSearch` object. `begin` takes in the following parameters:

* `doc`: `PDFDoc` object of the document to search
* `search_pattern`: text string or regex pattern to search
* `mode`: A number that encodes the search options, generated by bitwise ORing options together
* `start_page`: optional page number to start searching on. Defaults to 1
* `end_page`: optional page number for when to stop searching. Defaults to last page

The `mode` input is a number used for controlling how the search behaves. It can be created by using the "|" bitwise OR operation on the desired modes to use. All the mode can be found on the `PDFNet.TextSearch.Mode` object, they are:

* `e_reg_expression`: If set, treat the search pattern as a regular expression
* `e_case_sensitive`: If set, the text searched must match case of the search pattern
* `e_whole_word`: If set, only match whole words
* `e_search_up`: If set, search from the last page of the document backwards to the first
* `e_page_stop`: If set, will return a 'result' whenever a page has been searched
* `e_highlight`: If set, will return the quads of found results
* `e_ambient_string`: If set, will return text around the search pattern

After calling 'begin', calling [run](https://sdk.apryse.com/api/web/Core.PDFNet.TextSearch.html#run__anchor) will begin searching the document. It'll return a promise that resolves to a 'result' object with the with the following properties

* `e_ambient_string`: If using `e_ambient_string` mode, return characters surrounding the search pattern
* `code`: a `PDFNet.TextSearch.ResultCode` indicating whether the result is from a search term being found or the text search finished searching through a page or the document
  * `e_found`: search pattern found
  * `e_page`: done searching a page
  * `e_done`: done searching the whole document
* `highlights`: a [Highlights](https://sdk.apryse.com/api/web/Core.PDFNet.Highlights.html) object
* `out_str`: The string that matches the search term. Since sometimes case doesn't matter or regular expression could be use for searching, this could be different from the original search term
* `page_num`: The page the result was found on

If using `e_page_stop` mode, `run` will return a result whenever it has finished searching a page. Otherwise, it'll only return results when a match has been found or if the document has finished searching. After the first search result is returned, keep on calling `run` to get the next result until the search is complete.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/web/search/text-search-full-api.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
