Search Redact in ASP.NET Core PDF Viewer

14 Aug 20268 minutes to read

Search for a keyword in the loaded PDF and automatically add redaction annotations for each match. The example below calls findTextAsync to perform the search and then adds a redaction annotation for every returned text bound.

Prerequisites: Add the PdfViewer control to the ASP.NET Core application and ensure a document is loaded. Confirm the redaction feature is available in the used product version. Applying redaction permanently removes the selected content.

Steps to add Redaction annotations on search Text Bounds

Step 1: Follow the steps provided in the link to create a simple PDF Viewer sample.

Step 2: Use the following code-snippets to Add Redaction annotation on Search Text Bounds.

@page
@model IndexModel
@{
    ViewData["Title"] = "Search text and redact";
}
<div class="content-wrapper">
    <div style="margin-bottom:8px; display:flex; gap:8px; align-items:center;">
        <button id="searchTextRedact" type="button" onclick="searchTextAndRedact()">Search "syncfusion" & Mark for Redaction</button>
        <button id="applyRedaction" type="button" onclick="applyRedaction()">Apply Redaction</button>
    </div>

    <ejs-pdfviewer
        id="pdfViewer"
        style="height:640px; display:block"
        resourceUrl="https://cdn.syncfusion.com/ej2/31.2.12/dist/ej2-pdfviewer-lib"
        documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
        isExtractText="true">
    </ejs-pdfviewer>
</div>
<script type="text/javascript">
    window.onload = function () {
        window.viewer = document.getElementById('pdfViewer').ej2_instances[0];
        // Primary toolbar including Redaction
        viewer.toolbarSettings = {
            toolbarItems: [
                'OpenOption', 'UndoRedoTool', 'PageNavigationTool', 'MagnificationTool', 'PanTool',
                'SelectionTool', 'CommentTool', 'SubmitForm', 'AnnotationEditTool', 'RedactionEditTool',
                'FormDesignerEditTool', 'SearchOption', 'PrintOption', 'DownloadOption'
            ]
        };
    };

    // Finds "syncfusion" and adds redaction annotations for each match
    async function searchTextAndRedact() {
        if (!window.viewer) return;
        var term = 'syncfusion';
        try {
            var results = await viewer.textSearchModule.findTextAsync(term, false);
            if (!results || results.length === 0) {
                console.warn('No matches found.');
                return;
            }
            var px = function (pt) { return (pt * 96) / 72; };
            for (var i = 0; i < results.length; i++) {
                var pageResult = results[i];
                if (!pageResult || !pageResult.bounds || pageResult.bounds.length === 0) continue;
                if (pageResult.pageIndex == null) continue;
                var pageNumber = pageResult.pageIndex + 1; // 1-based
                for (var b = 0; b < pageResult.bounds.length; b++) {
                    var bound = pageResult.bounds[b];
                    viewer.annotation.addAnnotation('Redaction', {
                        bound: {
                            x: px(bound.x),
                            y: px(bound.y),
                            width: px(bound.width),
                            height: px(bound.height)
                        },
                        pageNumber: pageNumber,
                        overlayText: 'Confidential',
                        fillColor: '#00FF40FF',
                        fontColor: '#333333',
                        fontSize: 12,
                        fontFamily: 'Arial',
                        markerFillColor: '#FF0000',
                        markerBorderColor: '#000000'
                    });
                }
            }
        } catch (e) {
            console.error('Search failed:', e);
        }
    }

    // Permanently applies all redaction marks in the document
    function applyRedaction() {
        if (!window.viewer) return;
        viewer.annotation.redact();
    }
</script>

View Sample in GitHub

Notes

  • Ensure the PDF is fully loaded before triggering extraction and search.
  • Bounds from search are in points (72 DPI). Convert to pixels (96 DPI) to align with annotation coordinates.
  • Customize overlay text, colors, and typography as needed.
  • Adding a redaction annotation covers the content visually. To permanently remove sensitive data, use the viewer’s Apply Redaction action or equivalent API if available in the used product version.

See also