Skip to main content

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.

The text object is an instance accessible from the main MuPDFWebViewer instance as follows:
const text = webViewer.text;
This assumes you have returned your instance name as webViewer from the initMuPDFWebViewer() promise!

text

The text object has the following methods:
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

config
object
required
The configuration object.

Returns

result
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.
Example
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

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 from an LLM response.

Parameters

config
object
required
The configuration object.

Returns

result
Promise<{ pageIndex: number; words: [{ rects: TRect[] }] }>
required
A Promise that resolves to the matched word rects for highlighting.
Example
webViewer.text.locateSource({text:text}).then(
    function success(data) {
        console.log(data);
    },
    function failure(error) {
        alert(`Error locating text: ${error}`);
    }
);
Refer to Source Locator: Example for a more detailed example.

getSelected

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

result
Promise<{ text: string, pageIndex: number, rects: TRect[] } | undefined>
required
A Promise that resolves to the current selection, or undefined if there is no selection.
Example
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);