Calculates the phrase matches in the given page content based on the specified query and matching options.
An object containing the matches and their lengths.
matches - An array of character indices (character indices) on the page where matches were found.
matchesLength - An array of lengths for each matched text.
The query string to search for.
An object representing the line endings on the page, where keys are character indices and values indicate line endings.
The content of the page.
Whether to match the entire word only.
Whether the query should match phrases that start with the query string.
Whether the query should match phrases that end with the query string.
Whether the query allows wildcards for matching.
Determine if the search query constitutes a "whole word", by comparing the first/last character type with the preceding/following character type.
Helper for multi-term search that fills the matchesWithLength
array
and handles cases where one search term includes another search term (for
example, "tamed tame" or "this is"). It looks for intersecting terms in
the matches
and keeps elements with a longer match length.
Cancel search task.
// Open the document, find the text 'wildlife' and highlight the first result:
async function loadPdfViewer(selector) {
var viewer = new GcPdfViewer(selector, { restoreViewStateOnLoad: false });
viewer.addDefaultPanels();
var afterOpenPromise = new Promise((resolve)=>{ viewer.onAfterOpen.register(()=>{ resolve(); }); });
await viewer.open('wetlands.pdf');
await afterOpenPromise;
var findOptions = { Text: 'wildlife' };
var searchIterator = await viewer.searcher.search(findOptions);
var searchResult = await searchIterator.next();
viewer.searcher.cancel();
viewer.searcher.highlight(searchResult.value);
}
loadPdfViewer('#root');
Highlights a specified portion of text on a given page.
A promise that resolves to true
if the text was successfully highlighted, otherwise false
.
// Highlight the text from character 10 to character 20 on the first page with custom highlight colors.
viewer.highlightTextSegment(0, 10, 20, {
color: 'rgba(173, 216, 230, 0.5)', // semi-transparent light blue
borderColor: 'blue', // named color for the border
borderWidth: 4, // custom border width
clearPrevious: true
});
The index of the page where the text is located (0-based).
The starting character index (0-based) of the text segment to highlight.
The ending character index (0-based) of the text segment to highlight. The character at this index will be excluded from the highlight.
Optional
args: { borderColor?: string; borderWidth?: number; color?: string; paintHandler?: any }Optional parameters to customize the highlight, such as color, border color, and width.
Optional
borderOptional
borderThe width of the highlight border in pixels (default is 2 pixels).
Optional
color?: stringOptional
paintA custom function for handling the painting of the highlight (optional).
Retrieves the text rectangles and styles of a specific page.
The text rectangles and styles of the specified page.
The index of the page.
Navigates to a page containing the result and highlights found text.
// Open the document, find the text 'wildlife' and highlight the first result:
async function loadPdfViewer(selector) {
var viewer = new GcPdfViewer(selector, { restoreViewStateOnLoad: false });
viewer.addDefaultPanels();
var afterOpenPromise = new Promise((resolve)=>{ viewer.onAfterOpen.register(()=>{ resolve(); }); });
await viewer.open('wetlands.pdf');
await afterOpenPromise;
var findOptions = { Text: 'wildlife' };
var searchIterator = await viewer.searcher.search(findOptions);
var searchResult = await searchIterator.next();
viewer.searcher.cancel();
viewer.searcher.highlight(searchResult.value);
}
loadPdfViewer('#root');
Optional
pageIndex: numberIssues the next search result, primarily used internally by the PDF Searcher.
A Promise that resolves with the processed search result.
The search result to be processed.
The cancellation token to handle the possibility of cancellation.
Asynchronously generates search results based on the provided search options.
// Highlight all search results without opening SearchPanel.
const searchIterator = viewer.searcher.search({ Text: "test", MatchCase: true, HighlightAll: true });
searchIterator.next();
searcher.applyHighlight();
// Iterate all search results
const searcher = viewer.searcher;
var searchResults = [];
const searchIterator = searcher.search({ Text: textToSearch, MatchCase: true });
var searchResult = await searchIterator.next();
if (searchResult.value)
searcher.highlight(searchResult.value)
while (searchResult.value && !searchResult.done) {
const searchResultValue = searchResult.value;
searchResults.push(`index: ${searchResultValue.ItemIndex}, text: ${searchResultValue.DisplayText}, pageIndex: ${searchResultValue.PageIndex}`);
searchResult = await searchIterator.next();
}
console.log("Search results: " + (searchResults.length ? searchResults.join("; ") : "No search results"));
// Open the document, find the text 'wildlife' and highlight the first result:
async function loadPdfViewer(selector) {
var viewer = new DsPdfViewer(selector, { restoreViewStateOnLoad: false });
viewer.addDefaultPanels();
var afterOpenPromise = new Promise((resolve)=>{ viewer.onAfterOpen.register(()=>{ resolve(); }); });
await viewer.open('wetlands.pdf');
await afterOpenPromise;
var findOptions = { Text: 'wildlife' };
var searchIterator = await viewer.searcher.search(findOptions);
var searchResult = await searchIterator.next();
viewer.searcher.cancel();
viewer.searcher.highlight(searchResult.value);
}
loadPdfViewer('#root');
// Open the document, find the text 'wildlife' and print search results to the console:
async function loadPdfViewer(selector) {
var viewer = new DsPdfViewer(selector);
viewer.addDefaultPanels();
await viewer.open('wetlands.pdf');
await (new Promise((resolve)=>{
viewer.onAfterOpen.register(()=>{
resolve();
});
}));
var findOptions = {
Text: 'wildlife',
MatchCase: true,
WholeWord: true,
StartsWith: false,
EndsWith: false,
Wildcards: false,
Proximity: false,
SearchBackward: false,
HighlightAll: true
};
var searcher = viewer.searcher;
var searchIterator = await searcher.search(findOptions);
var resultsCount = 0;
var searchResult;
do {
searchResult = await searchIterator.next();
if (searchResult.value) {
// this could be either result or progress message (ItemIndex < 0)
if(searchResult.value.ItemIndex >= 0) {
console.log('next search result:');
console.log(searchResult.value);
resultsCount++;
} else {
const pageCount = _doc.pageCount.totalPageCount || _doc.pageCount.renderedSoFar;
console.log('search progress, page index is ' + searchResult.value.PageIndex);
}
}
else {
console.log("Search completed");
break;
}
}
while(!searchResult.done);
console.log('Total results count is ' + resultsCount);
}
An asynchronous iterable iterator that yields search results.
Search options to customize the search.
The (current) normalized search query.
Gets highlightAll option.
Sets highlightAll option.
Gets selected search result.
Gets selected search result index. Returns -1 if nothing is selected.
Retrieves non-empty searcher options.
The current state of the PDF searcher options, ensuring that it is a valid object.
Gets total search results count.
Gets total search results count promise.
PDF document searcher. Generates asynchronous search results (see search method). The GcPdfSearcher API was designed to be used internally with Search Panel UI.