...master 2026-11-12 (1037e2069+unstaged)

StructuredText

StructuredText objects hold text from a page that has been analyzed and grouped into blocks, lines and spans.

Constructors

class StructuredText()

You cannot create instances of this class with the new operator!

To obtain a StructuredText instance use Page.prototype.toStructuredText().

Instance methods

StructuredText.prototype.search(needle, maxHits)

Search the text for all instances of needle, and return an array with all matches found on the page.

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

var result = sText.search("Hello World!")

StructuredText.prototype.highlight(p, q, maxHits)

Return an array of Quad used to highlight a selection defined by the start and end points.

var result = sText.highlight([100, 100], [200, 100])

StructuredText.prototype.copy(p, q)

Return the text from the selection defined by the start and end points.

var result = sText.copy([100, 100], [200, 100])

StructuredText.prototype.walk(walker)

Walk through the blocks (images or text blocks) of the structured text. For each text block walk over its lines of text, and for each line each of its characters. For each block, line or character the walker will have a method called.

var sText = page.toStructuredText()
sText.walk({
        beginLine: function (bbox, wmode, direction) {
                console.log("beginLine", bbox, wmode, direction)
        },
        endLine: function () {
                console.log("endLine")
        },
        beginTextBlock: function (bbox) {
                console.log("beginTextBlock", bbox)
        },
        endTextBlock: function () {
                console.log("endTextBlock")
        },
        beginStruct: function (standard, raw, index) {
                console.log("beginStruct", standard, raw, index)
        },
        endStruct: function () {
                console.log("endStruct")
        },
        onChar: function (utf, origin, font, size, quad, argb, flags) {
                console.log("onChar", utf, origin, font, size, quad, argb, flags)
        },
        onImageBlock: function (bbox, transform, image) {
                console.log("onImageBlock", bbox, transform, image)
        },
        onVector: function (bbox, flags, argb) {
                console.log("onVector", bbox, flags, argb)
        },
})

StructuredText.prototype.asText()

Returns a plain text representation.

StructuredText.prototype.asHTML(id)

Returns a string containing an HTML rendering of the text.

StructuredText.prototype.asJSON(scale)

Returns a JSON string representing the structured text data.

This is a simplified serialization of the information that StructuredText.prototype.walk() provides.

Note: You must extract the structured text with “preserve-spans”! If you forget to set this option, any font changes in the middle of the line will not be present in the JSON output.

var data = JSON.parse(page.toStructuredText("preserve-spans").asJSON())