Perform a search

The JavaScript API functions described here allow you to launch a textual search on an open document, to highlight and position yourself on specific text elements on the ARender viewer.

To use them from an application other than ARender, simply make the call from the iframe’s window object. The code to access this object will vary slightly depending on the framework used. Below is an example of a simple search for the term “ARender.” To navigate to the next result, you can call the same function again. For more information on simple search and advanced search usage, refer to the documentation mentioned above.

const iframeWindow = iframeRef.current.contentWindow
iframeWindow.getARenderJS().getSearchJSAPI().askSearchTextNext("arender")
const iframeWindow = this.iframeRef.nativeElement.contentWindow;
iframeWindow.getARenderJS().getSearchJSAPI().askSearchTextNext("arender")
/**
 * Returns the ARenderJS object from the iframe.
 * Throws an error if the iframe is not accessible or not loaded.
 */
function getARenderJS() {
  const iframeWindow = iframeRef.value?.contentWindow
    ? iframeRef.value.contentWindow
    : undefined

  if (iframeWindow) {
    return iframeWindow.getARenderJS()
  }
  throw new Error("The iframe is not accessible or not loaded.")
}

function askSearchTextNext(text) {
  getARenderJS()
    .getSearchJSAPI()
    .askSearchTextNext(text)
}
/**
 * Expose the `askSearchTextNext` function to the parent component.
 */
defineExpose({
  askSearchTextNext
})
/**
 * Returns the ARenderJS object from the iframe.
 * Throws an error if the iframe is not accessible or not loaded.
 */
function getARenderJS() {
  const iframeWindow = iframeRef?.contentWindow
    ? iframeRef.contentWindow
    : undefined

  if (iframeWindow) {
    return iframeWindow.getARenderJS()
  }
  throw new Error("The iframe is not accessible or not loaded.")
}
/**
 * Asks ARender to search for the next occurrence of the given text.
 * @param text The text to search for.
 */
export function askSearchTextNext(text) {
  getARenderJS()
    .getSearchJSAPI()
    .askSearchTextNext(text)
}

function search() {
    const iframe = document.getElementById('arender-iframe');
    if (iframe && iframe.contentWindow) {
        const iframeWindow = iframe.contentWindow;
        iframeWindow.getARenderJS().getSearchJSAPI().askSearchTextNext("arender");
    } else {
        console.error('Iframe or contentWindow is not accessible.');
    }
}