Find text with findTextAsync in the ASP.NET Core PDF Viewer

The findTextAsync method searches for a string or array of strings asynchronously and returns bounding rectangles for each match. Use it to locate text positions across the document or on a specific page.

Here is an example of how to use findTextAsync:

@page "{handler?}"
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <button onclick="findText()">Find Text</button>
    <button onclick="findTexts()">Find Multiple Texts</button>
    <ejs-pdfviewer id="pdfviewer" style="height:600px" resourceUrl="https://cdn.syncfusion.com/ej2/29.1.33/dist/ej2-pdfviewer-lib" documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf">
    </ejs-pdfviewer>
</div>

<script type="text/javascript">
    function findText() {
        var viewer = document.getElementById('pdfviewer').ej2_instances[0];
        // Search for a single text ('pdf') across all pages (case insensitive)
        viewer.textSearchModule.findTextAsync('pdf', false).then(function (res) {
            console.log(res);  // Log the search results for 'pdf'
        });
    }

    function findTexts() {
        var viewer = document.getElementById('pdfviewer').ej2_instances[0];
        // Search for multiple texts (['pdf', 'the']) across all pages (case insensitive)
        viewer.textSearchModule.findTextAsync(['pdf', 'the'], false).then(function (res) {
            console.log(res);  // Log the search results for 'pdf' and 'the'
        });
    }
</script>

Description

The findTextAsync method performs an asynchronous text search within a PDF document. You can search for a single string or multiple strings while controlling case sensitivity. By default, the search runs across all pages. Specify the optional pageIndex argument to limit the search to a single page.

Parameters

  • **text (string string[]):** String or array of strings to search for.
  • matchCase (boolean): Whether the search is case-sensitive. true matches exact case; false ignores case.

  • pageIndex (optional, number): Zero-based page index to search. If omitted, searches all pages.

Example workflow

  • findTextAsync(‘pdf’, false): Searches for “pdf” case-insensitively across all pages.

  • findTextAsync([‘pdf’, ‘the’], false): Searches for “pdf” and “the” case-insensitively across all pages.

  • findTextAsync(‘pdf’, false, 0): Searches for “pdf” case-insensitively on page 0 only.

  • findTextAsync([‘pdf’, ‘the’], false, 1): Searches for “pdf” and “the” case-insensitively on page 1 only.

View sample in GitHub