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 (ornullif 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()orSearch.prototype.searchBackwards()are called, but is normally done whenever either of those two functions returnsEach 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:
var result = search.searchForwards()
- Search.prototype.searchBackwards()¶
Searches backwards from the current position in the document for the next match, if any.
- Returns:
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);
}
}
}