Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
MuPDF 1.28.0
Logo
MuPDF 1.28.0

Guides

  • What is MuPDF?
  • License
  • Installing
  • Using with Javascript
  • Using with Java
  • Using with Android
  • Tools
    • mupdf-gl
    • mutool
    • mutool audit
    • mutool bake
    • mutool barcode
    • mutool clean
    • mutool convert
    • mutool create
    • mutool draw
    • mutool extract
    • mutool grep
    • mutool info
    • mutool merge
    • mutool pages
    • mutool poster
    • mutool recolor
    • mutool run
    • mutool show
    • mutool sign
    • mutool trace
    • mutool trim

Cookbook

  • MuPDF Explored
  • C Examples
    • Render Images
    • Multi-threaded
    • Stories
  • Progressive Loading
  • Javascript Examples
    • Basics
    • Advanced

Reference

  • Concepts
    • Glossary
    • Coordinate Systems
    • Option Strings
    • Document Writer Options
    • PDF Write Options
    • Structured Text Options
  • C Library
    • Introduction
    • Overview
    • Coding Style
    • Fitz
      • API Overview
      • Colors
      • Context
      • Data Structures
      • Errors
      • Buffers & Streams
      • Math
      • Memory
      • Pixmaps
      • Strings
      • XML Parser
    • C API
  • Javascript Library
    • Introduction
    • Functions
    • Types
      • Archive
      • Buffer
      • Color
      • ColorSpace
      • DOM
      • DefaultColorSpaces
      • Device
      • DisplayList
      • DisplayListDevice
      • Document
      • DocumentWriter
      • DrawDevice
      • Font
      • Image
      • Link
      • LinkDestination
      • Matrix
      • MultiArchive
      • OutlineItem
      • OutlineIterator
      • Page
      • Path
      • PathWalker
      • Pixmap
      • Point
      • Quad
      • Rect
      • Search
      • SearchResult
      • Shade
      • Story
      • StrokeState
      • StructuredText
      • StructuredTextWalker
      • Text
      • TextWalker
      • TreeArchive
      • PDFAnnotation
      • PDFDocument
      • PDFFilespecParams
      • PDFGraftMap
      • PDFObject
      • PDFPage
      • PDFProcessor
      • PDFWidget
  • C++, Python, and C#

Other

  • Change Log
  • Third Party Libraries
Back to top

Search¶

Search objects hold the current search position in a Document, the search pattern and the options related to a search in a StructuredText object.

Constructors¶

class Search(needle, options)¶

Create a new search looking for the specified pattern according to the given options.

Arguments:
  • needle (string) – Pattern to search for.

  • options (string) – A comma-separated string of search options, see Search Options.

Constants¶

Search.MORE_INPUT¶

indicates that Search.prototype.feedPage() should be called with the StructuredText (or null if not available) of the desired page.

Search.MATCH¶

indicates that a search match has been found.

Search.COMPLETE¶

indicatres that the search has reached either the end, or the beginning, of the document without any found match.

Instance methods¶

Search.prototype.feedPage(stextPage, pageNumber)¶

Feed given StructuredText page with the given page number to the search.

This can be done for the initial page before Search.prototype.searchForwards() or Search.prototype.searchBackwards() are called, but is normally done whenever either of those two functions returns

Each match in the result is an array containing one or more Quads that cover the matching text.

var result = searchForwards();
if (result.reason == Search.MORE_INPUT)
        search.feedPage(doc.loadPage(result.needPage).toStructuredText(), result.needPage);
Search.prototype.searchForwards()¶

Searches forwards from the current position in the document for the next match, if any.

Returns:

SearchResult

var result = search.searchForwards()
Search.prototype.searchBackwards()¶

Searches backwards from the current position in the document for the next match, if any.

Returns:

SearchResult

var result = search.searchBackwards()

Example¶

var filename = "pdfref17.pdf"
var doc = Document.openDocument(filename);

var search = new Search("baseline", "ignore-case");
var firstPageToSearch = 500;
search.feedPage(doc.loadPage(firstPageToSearch).toStructuredText(), firstPageToSearch);

while (true)
{
        var result = search.searchForwards();
        if (result.reason == Search.COMPLETE)
                break;
        else if (result.reason == Search.MORE_INPUT)
        {
                if (result.needPage >= 0 && result.needPage < doc.countPages())
                        search.feedPage(doc.loadPage(result.needPage).toStructuredText(), result.needPage);
                else
                        search.feedPage(null, result.needPage);
        }
        else if (result.reason == Search.MATCH)
        {
                var begin = result.begin;
                var end = result.end;
                var page = -1;
                var block = -1;
                var line = -1;
                var char = -1;
                var capture = false;
                var str = "";
                var walker = {
                        beginTextBlock: function (rect, props) {
                                block++;
                        },
                        beginLine: function (rect, wmode, dir) {
                                line++;
                                str = "";
                                if (page == begin.page && block == begin.block && line == begin.line)
                                        capture = true;
                        },
                        onChar: function (utf, origin, font, size, quad, color) {
                                char++;
                                if (page == begin.page && block == begin.block && line == begin.line && char == begin.char)
                                        str = str + "{";
                                str = str + utf;
                                if (page == end.page && block == end.block && line == end.line && char == end.char)
                                        str = str + "}";
                        },
                        endLine: function () {
                                char = -1;
                                if (capture)
                                        console.log(filename + ":" + (page+1) + ":" + (block+1) + ":" + (line+1) + ":" + str);
                                if (page == end.page && block == end.block && line == end.line)
                                        capture = false;
                        },
                        endTextBlock: function () {
                                line = -1;
                        },
                };

                for (var pageno = begin.page; pageno <= end.page; pageno++)
                {
                        page = pageno;
                        block = -1;
                        doc.loadPage(pageno).toStructuredText().walk(walker);
                }
        }
}
Next
SearchResult
Previous
Rect
Copyright © 2004-2026 Artifex
Made with Furo
Find #mupdf on Discord
On this page
  • Search
    • Constructors
      • Search
    • Constants
      • MORE_INPUT
      • MATCH
      • COMPLETE
    • Instance methods
      • feedPage
      • searchForwards
      • searchBackwards
    • Example