# GcPdfSearcher

## Content

# Class: GcPdfSearcher

PDF document searcher.
Generates asynchronous search results (see search method).
The GcPdfSearcher API was designed to be used internally with Search Panel UI.

## Accessors

### \_query

#### Get Signature

```ts
get _query(): string;
```

##### Returns

`string`

The (current) normalized search query.

***

### highlightAll

#### Get Signature

```ts
get highlightAll(): boolean;
```

Gets highlightAll option.

##### Returns

`boolean`

#### Set Signature

```ts
set highlightAll(checked): void;
```

Sets highlightAll option.

##### Parameters

###### checked

`boolean`

##### Returns

`void`

***

### selectedSearchResult

#### Get Signature

```ts
get selectedSearchResult(): SearchResult;
```

Gets selected search result.

##### Returns

`SearchResult`

***

### selectedSearchResultIndex

#### Get Signature

```ts
get selectedSearchResultIndex(): number;
```

Gets selected search result index. Returns -1 if nothing is selected.

##### Returns

`number`

***

### state

#### Get Signature

```ts
get state(): PdfSearcherOptions;
```

Retrieves non-empty searcher options.

##### Returns

`PdfSearcherOptions`

The current state of the PDF searcher options, ensuring that it is a valid object.

***

### totalResultsCount

#### Get Signature

```ts
get totalResultsCount(): number;
```

Gets total search results count.

##### Returns

`number`

***

### totalResultsCountPromise

#### Get Signature

```ts
get totalResultsCountPromise(): Promise<number>;
```

Gets total search results count promise.

##### Returns

`Promise`&lt;`number`&gt;

## Methods

### \_calculatePhraseMatch()

```ts
_calculatePhraseMatch(
   query, 
   pageContentsEndings, 
   pageContent, 
   entireWord, 
   startsWith, 
   endsWith, 
   wildcards): object;
```

Calculates the phrase matches in the given page content based on the specified query and matching options.

#### Parameters

##### query

`string`

The query string to search for.

##### pageContentsEndings

An object representing the line endings on the page, where keys are character indices and values indicate line endings.

##### pageContent

`string`

The content of the page.

##### entireWord

`boolean`

Whether to match the entire word only.

##### startsWith

`boolean`

Whether the query should match phrases that start with the query string.

##### endsWith

`boolean`

Whether the query should match phrases that end with the query string.

##### wildcards

`boolean`

Whether the query allows wildcards for matching.

#### Returns

`object`

An object containing the matches and their lengths.

##### matches

```ts
matches: number[];
```

##### matchesLength

```ts
matchesLength: number[];
```

***

### \_extractText()

```ts
_extractText(): void;
```

Extract all text from pdf document once.

#### Returns

`void`

***

### \_isEntireWord()

```ts
_isEntireWord(
   content, 
   lineEndings, 
   startIdx, 
   length): boolean;
```

Determine if the search query constitutes a "whole word", by comparing the
first/last character type with the preceding/following character type.

#### Parameters

##### content

`any`

##### lineEndings

##### startIdx

`any`

##### length

`any`

#### Returns

`boolean`

***

### \_prepareMatches()

```ts
_prepareMatches(
   matchesWithLength, 
   matches, 
   matchesLength): void;
```

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.

#### Parameters

##### matchesWithLength

`any`

##### matches

`any`

##### matchesLength

`any`

#### Returns

`void`

***

### applyHighlight()

```ts
applyHighlight(): void;
```

Render highlight for the current search result.

#### Returns

`void`

***

### cancel()

```ts
cancel(): void;
```

Cancel search task.

#### Returns

`void`

#### Example

```javascript
// 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');
```

***

### createHighlightFromTextSegment()

```ts
createHighlightFromTextSegment(
   pageIndex, 
   startCharIndex, 
   endCharIndex, 
   args?): Promise<ICustomHighlight>;
```

Highlights a specified portion of text on a given page.

#### Parameters

##### pageIndex

`number`

The index of the page where the text is located (0-based).

##### startCharIndex

`number`

The starting character index (0-based) of the text segment to highlight.

##### endCharIndex

`number`

The ending character index (0-based) of the text segment to highlight.
The character at this index will be excluded from the highlight.

##### args?

Optional parameters to customize the highlight, such as color, border color, and width.

###### borderColor?

`string`

The color of the highlight border, specified in `rgba`, `hex`, or named color format.
Default is a semi-transparent orange (`rgba(255, 165, 0, 0.75)` or `#FFA500`) which provides a contrasting border.

###### borderWidth?

`number`

The width of the highlight border in pixels (default is 2 pixels).

###### color?

`string`

The fill color for the highlight, specified in `rgba`, `hex`, or named color format.
Default is a semi-transparent yellow (`rgba(255, 255, 0, 0.5)` or `#FFFF00`) which provides a clear highlight.

###### paintHandler?

`any`

A custom function for handling the painting of the highlight (optional).

#### Returns

`Promise`&lt;[`ICustomHighlight`](../interfaces/ICustomHighlight)&gt;

A promise that resolves to `true` if the text was successfully highlighted, otherwise `false`.

#### Example

```javascript
// 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
});
```

***

### fetchPageContent()

```ts
fetchPageContent(pageIndex): Promise<string>;
```

Retrieves the content of a specific page.

#### Parameters

##### pageIndex

`number`

The index of the page.

#### Returns

`Promise`&lt;`string`&gt;

The content of the specified page.

***

### fetchPageContentLineEndings()

```ts
fetchPageContentLineEndings(pageIndex): Promise<{
[x: number]: boolean;
}>;
```

Retrieves the line endings of the content of a specific page.

#### Parameters

##### pageIndex

`number`

The index of the page.

#### Returns

`Promise`&lt;\{
[`x`: `number`]: `boolean`;
\}&gt;

The line endings of the content of the specified page.

***

### fetchPageTextRects()

```ts
fetchPageTextRects(pageIndex): Promise<{
  items: IGcTextRect[];
  styles: any[];
}>;
```

Retrieves the text rectangles and styles of a specific page.

#### Parameters

##### pageIndex

`number`

The index of the page.

#### Returns

`Promise`&lt;\{
  `items`: `IGcTextRect`[];
  `styles`: `any`[];
\}&gt;

The text rectangles and styles of the specified page.

***

### getContentSnippet()

```ts
getContentSnippet(
   startInd, 
   endInd, 
   pageContent, 
   pageContentsEndings, 
   fillLineEndingsWithSpaces?): string;
```

Extracts a substring from `pageContent` between `startInd` and `endInd`, and optionally fills line endings with spaces
based on `pageContentsEndings` mapping.

#### Parameters

##### startInd

`number`

The start index of the substring in `pageContent`.

##### endInd

`number`

The end index of the substring in `pageContent`.

##### pageContent

`string`

The full content of the page from which the substring is extracted.

##### pageContentsEndings

A mapping indicating where line endings occur.

##### fillLineEndingsWithSpaces?

`boolean`

Whether to fill line endings with spaces if they don't already exist.

#### Returns

`string`

The substring with optional spaces inserted at line breaks.

***

### hashSearchResultId()

```ts
hashSearchResultId(searchResult): string;
```

Generates a unique hash ID for the given search result.

#### Parameters

##### searchResult

`SearchResult`

The search result object to generate a hash ID for.

#### Returns

`string`

A unique hash ID based on the properties of the search result.

***

### highlight()

```ts
highlight(searchResult, pageIndex?): Promise<void>;
```

Navigates to a page containing the result and highlights found text.

#### Parameters

##### searchResult

`any`

##### pageIndex?

`number`

#### Returns

`Promise`&lt;`void`&gt;

#### Example

```javascript
// 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');
```

***

### isResultSelected()

```ts
isResultSelected(result): boolean;
```

Checks whether a specific search result is currently selected.

#### Parameters

##### result

`SearchResult`

The search result to be checked for selection.

#### Returns

`boolean`

A boolean indicating whether the provided search result is currently selected.

***

### nextSearchResult()

```ts
nextSearchResult(result, cancellation): Promise<SearchResult>;
```

Issues the next search result, primarily used internally by the PDF Searcher.

#### Parameters

##### result

`SearchResult`

The search result to be processed.

##### cancellation

`any`

The cancellation token to handle the possibility of cancellation.

#### Returns

`Promise`&lt;`SearchResult`&gt;

A Promise that resolves with the processed search result.

***

### resetResults()

```ts
resetResults(): void;
```

Clear search results.
This method must be called when the SearchPanel is closed.

#### Returns

`void`

***

### search()

```ts
search(options): AsyncIterableIterator<SearchResult>;
```

Asynchronously generates search results based on the provided search options.

#### Parameters

##### options

`FindOptions`

Search options to customize the search.

#### Returns

`AsyncIterableIterator`&lt;`SearchResult`&gt;

An asynchronous iterable iterator that yields search results.

#### Examples

```javascript
 // Highlight all search results without opening SearchPanel.
 const searchIterator = viewer.searcher.search({ Text: "test", MatchCase: true, HighlightAll: true });
 searchIterator.next();
 searcher.applyHighlight();
```

```javascript
 // 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"));
```

```javascript
// 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:
```javascript
 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);
 }
```

***

### toggle()

```ts
toggle(forceExpand?, replaceMode?): void;
```

Toggles the visibility of the Search UI.

#### Parameters

##### forceExpand?

`boolean`

Indicates whether to force expanding the Search UI. Default is true.

##### replaceMode?

`boolean`

Enables the replace mode in the Search UI. Default is false.

#### Returns

`void`

#### Implementation of

```ts
IPdfSearcher.toggle
```

***

### updateAllPages()

```ts
updateAllPages(): void;
```

Repaint highlight for visible pages.

#### Returns

`void`
