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

text

The text object has the following methods: search(config: { keyword: string; caseSensitive?: boolean; useRegex?: boolean; pageRange?: string }) Searches for text.
  • Arguments:
    • config – The configuration object.
Config object:
  • Arguments:
    • keyword(required) Search keyword.
    • caseSensitive(optional) Whether to be case sensitive.
    • useRegex(optional) Whether to use regular expressions.
    • pageRange(optional) Page index range to search (e.g., “1-5, 7, 9-12”, “all”). If not set will search all pages.
  • Returns: Promise<{ results: { words: { prefix: string; keyword: string; suffix: string; redMarked: boolean; rects: mupdfwv.TRect[]; }[]; pageIndex: number; }[]; }.
Example
mupdf.text.search({keyword:"hello"});

// returns e.g.

{
    results: {
        words: {
            prefix: string;
            keyword: string;
            suffix: string;
            rects: {
                    top: number;
                    left: number;
                    bottom: number;
                    right: number;
                }[];
        }[];
    }[];
}

getSelected

getSelected() Gets the currently selected text from a document. If there is no selected text then returns null.
  • Returns: Promise<{ text: string, pageIndex: number, rects: TRect[] }>.
Example
async function getSelectedText() {
    return mupdf.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);
I