Extract text using extractTextCompleted in ASP.NET Core PDF Viewer

28 Feb 20261 minute to read

The PDF Viewer provides the isExtractText property and the extractTextCompleted event to retrieve all text content and its corresponding bounding box coordinates from a document.

Enable text extraction

To extract text, set the isExtractText property to true. Once the text extraction process finishes, the extractTextCompleted event is triggered, providing access to the documentTextCollection.

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

<div class="text-center">
    <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" isExtractText=true>
    </ejs-pdfviewer>
</div>

<script type="text/javascript">
    document.addEventListener('DOMContentLoaded', function () {
        var viewer = document.getElementById('pdfviewer').ej2_instances[0];
        viewer.isExtractText = true;
        viewer.extractTextCompleted = args => {
        //Extract the Complete text of load document
        console.log(args);
        console.log(args.documentTextCollection[1]);
        //Extract the Text data.
        console.log(args.documentTextCollection[1][1].TextData);
        //Extract Text in the Page.
        console.log(args.documentTextCollection[1][1].PageText);
        //Extracts the first text of the PDF document along with its bounds
        console.log(args.documentTextCollection[1][1].TextData[0].Bounds);
    };
    });
</script>

View sample in GitHub