> ## Documentation Index
> Fetch the complete documentation index at: https://webviewer-docs.mupdf.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Text API

The [`text`](#text) object is an instance accessible from the main [MuPDFWebViewer](/api-reference/introduction#initmupdfwebviewer) instance as follows:

```javascript theme={null}
const text = webViewer.text;
```

<Note>
  This assumes you have returned your instance name as `webViewer` from the [initMuPDFWebViewer()](/api-reference/introduction#initmupdfwebviewer) promise!
</Note>

## text

The `text` object has the following methods:

### search

```typescript theme={null}
search(config: {
  keyword: string;
  caseSensitive?: boolean;
  useRegex?: boolean;
  pageRange?: string;
}): Promise<{
  results: {
    words: {
      prefix: string;
      keyword: string;
      suffix: string;
      redMarked: boolean;
      rects: TRect[];
    }[];
    pageIndex: number;
  }[];
}>;
```

Searches for text.

#### Parameters

<ParamField body="config" type="object" required>
  The configuration object.
</ParamField>

<Expandable title="config properties">
  <ParamField body="config.keyword" type="string" required>
    Search keyword.
  </ParamField>

  <ParamField body="config.caseSensitive" type="boolean">
    Whether to be case sensitive.
  </ParamField>

  <ParamField body="config.useRegex" type="boolean">
    Whether to use regular expressions.
  </ParamField>

  <ParamField body="config.pageRange" type="string">
    Page index range to search (e.g., `"0-5, 7, 9-12"`, `"all"`). Page references are zero-indexed. If omitted, searches all pages.
  </ParamField>
</Expandable>

#### Returns

<ResponseField name="result" type="Promise<{ results: { words: { prefix: string; keyword: string; suffix: string; redMarked: boolean; rects: TRect[] }[], pageIndex: number }[] }" required>
  A Promise that resolves to search results grouped by page.
</ResponseField>

**Example**

```javascript theme={null}
webViewer.text.search({keyword:"hello"});

// returns e.g.

{
    results: [
      {
        pageIndex:number,
        words: [
          {
            prefix: string;
            keyword: string;
            suffix: string;
            redMarked: boolean;
            rects: {
                    top: number;
                    left: number;
                    bottom: number;
                    right: number;
                }[];
          }
        ];
      }
    ];
}
```

### locateSource

```typescript theme={null}
locateSource(config: { text: string; pageRange?: string }): Promise<{
  pageIndex: number;
  words: [{ rects: TRect[] }];
}>;
```

Searches for the given text and returns its coordinates for highlighting. This is designed to be used with LLM-generated answers that include quoted source text, allowing you to map that text back to the PDF for highlighting.

The input text is expected to be [high-quality Markdown](/ai/index#flow) from an LLM response.

#### Parameters

<ParamField body="config" type="object" required>
  The configuration object.
</ParamField>

<Expandable title="config properties">
  <ParamField body="config.text" type="string" required>
    Query text.
  </ParamField>

  <ParamField body="config.pageRange" type="string">
    Page index range to search (e.g., `"0-5, 7, 9-12"`, `"all"`). Page references are zero-indexed. If omitted, searches all pages.
  </ParamField>
</Expandable>

#### Returns

<ResponseField name="result" type="Promise<{ pageIndex: number; words: [{ rects: TRect[] }] }>" required>
  A Promise that resolves to the matched word rects for highlighting.
</ResponseField>

**Example**

```javascript theme={null}
webViewer.text.locateSource({text:text}).then(
    function success(data) {
        console.log(data);
    },
    function failure(error) {
        alert(`Error locating text: ${error}`);
    }
);
```

<Tip>
  Refer to [Source Locator: Example](/ai/index#example) for a more detailed example.
</Tip>

### getSelected

```typescript theme={null}
getSelected(): Promise<{ text: string; rects: TRect[]; pageIndex: number } | undefined>;
```

Gets the currently selected text from a document.

If there is no selected text then it returns `undefined`.

#### Returns

<ResponseField name="result" type="Promise<{ text: string, pageIndex: number, rects: TRect[] } | undefined>" required>
  A Promise that resolves to the current selection, or `undefined` if there is no selection.
</ResponseField>

**Example**

```javascript theme={null}
async function getSelectedText() {
    return webViewer.text.getSelected();
}

function success(result) {
    if (result) {
        console.log(`Text is: ${result.text}`);
        console.log(`Page index is: ${result.pageIndex}`);
        let rects = result.rects;
        console.log(`rect[0] metrics:\ntop=${rects[0].top}\nright=${rects[0].right}\nbottom=${rects[0].bottom}\nleft=${rects[0].left}`);
    }
}

function failure(error) {
    console.error(`Error: ${error}`);
}

getSelectedText().then(success, failure);
```
