# pdf-viewer-redaction

> PDF viewer sample marking sensitive regions or searched text for permanent redaction.

**Framework:** javascript  **Component:** PdfViewer  **Variant:** redaction

## Get this item

**If you are an agent, fetch the JSON.** Source is inlined, so one request is enough and no tooling is required:

```
GET https://ai.syncfusion.com/r/javascript/pdf-viewer-redaction.json
```

**Install Package(s)**

```bash
npm install @syncfusion/ej2-pdfviewer
```

**Notes**

- Syncfusion release: 2026 Volume 3 (v35.1.37)
- The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md

## Source files

### src/components/pdf-viewer-redaction/pdf-viewer.html

```html
<div class="control-section redaction-sample">
    <div class="redaction-toolbar" role="group" aria-label="Redaction controls">
        <button id="addRedaction" class="e-btn e-primary" type="button" disabled>Add Redaction</button>
        <label for="redactionSearch">Text to redact</label>
        <input id="redactionSearch" class="e-input" type="search" placeholder="Search PDF text" disabled />
        <button id="searchRedaction" class="e-btn" type="button" disabled>Search &amp; Mark</button>
        <button id="applyRedactions" class="e-btn e-danger" type="button" disabled>Apply Redactions</button>
    </div>
    <p id="redactionStatus" class="redaction-status" role="status">Loading PDF document...</p>
    <div id="redaction-viewer" style="height:640px; width:100%;"></div>
</div>
<style>
    .redaction-sample { padding: 8px; }
    .redaction-toolbar { display:flex; flex-wrap:wrap; align-items:end; gap:12px; margin-bottom:8px; }
    .redaction-toolbar label { font-weight:600; margin-left:4px; }
    .redaction-toolbar input { width:220px; }
    .redaction-status { min-height:20px; margin:4px 0 10px; }
    @media (max-width: 720px) {
        .redaction-toolbar input { width:100%; }
        .redaction-toolbar label { width:100%; margin-left:0; }
    }
</style>

```

### src/components/pdf-viewer-redaction/pdf-viewer.ts

```typescript
import {
    Annotation, BookmarkView, FormDesigner, FormFields, LinkAnnotation, Magnification,
    Navigation, PageOrganizer, PdfViewer, Print, TextSearch, TextSelection, ThumbnailView, Toolbar
} from '@syncfusion/ej2-pdfviewer';
import type { RedactionSettings } from '@syncfusion/ej2-pdfviewer';

PdfViewer.Inject(
    Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,
    Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner, PageOrganizer
);

export function initPdfViewerRedaction(): void {
    let isApplyingRedactions = false;
    let isDocumentLoaded = false;
    let isSearching = false;
    const viewer = new PdfViewer({
        documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf',
        resourceUrl: 'https://cdn.syncfusion.com/ej2/35.1.37/dist/ej2-pdfviewer-lib',
        isExtractText: true,
        toolbarSettings: {
            showTooltip: true,
            toolbarItems: [
                'OpenOption', 'UndoRedoTool', 'PageNavigationTool', 'MagnificationTool',
                'PanTool', 'SelectionTool', 'CommentTool', 'SubmitForm', 'AnnotationEditTool',
                'RedactionEditTool', 'FormDesignerEditTool', 'SearchOption', 'PrintOption',
                'DownloadOption'
            ]
        }
    });
    const addButton = document.getElementById('addRedaction') as HTMLButtonElement;
    const searchInput = document.getElementById('redactionSearch') as HTMLInputElement;
    const searchButton = document.getElementById('searchRedaction') as HTMLButtonElement;
    const applyButton = document.getElementById('applyRedactions') as HTMLButtonElement;
    const status = document.getElementById('redactionStatus') as HTMLElement;

    const updateControls = (): void => {
        addButton.disabled = !isDocumentLoaded;
        searchInput.disabled = !isDocumentLoaded || isSearching;
        searchButton.disabled = !isDocumentLoaded || isSearching;
        applyButton.disabled = !isDocumentLoaded;
    };

    viewer.documentLoad = (): void => {
        viewer.toolbar.showRedactionToolbar(true);
        isDocumentLoaded = true;
        status.textContent = isApplyingRedactions
            ? 'Redactions applied to the document.'
            : 'Document loaded. Add a redaction or search for text to mark.';
        isApplyingRedactions = false;
        updateControls();
    };

    addButton.addEventListener('click', (): void => {
        viewer.annotation.addAnnotation('Redaction', {
            bound: { x: 150, y: 400, width: 200, height: 40 },
            pageNumber: viewer.currentPageNumber,
            overlayText: 'Confidential',
            fillColor: '#000000',
            fontColor: '#FFFFFF'
        } as RedactionSettings);
        status.textContent = 'Redaction added to the current page. Apply Redactions to make it permanent.';
    });

    searchButton.addEventListener('click', async (): Promise<void> => {
        const term = searchInput.value.trim();
        if (!term) {
            status.textContent = 'Enter text to search for and redact.';
            return;
        }

        isSearching = true;
        updateControls();
        status.textContent = `Searching for "${term}"...`;
        try {
            const results = await viewer.textSearchModule.findTextAsync(term, false);
            let redactionCount = 0;
            for (const result of results) {
                if (typeof result.pageIndex !== 'number' || !result.bounds?.length) {
                    continue;
                }
                for (const bound of result.bounds) {
                    viewer.annotation.addAnnotation('Redaction', {
                        bound: {
                            x: bound.x * (96 / 72),
                            y: bound.y * (96 / 72),
                            width: bound.width * (96 / 72),
                            height: bound.height * (96 / 72)
                        },
                        pageNumber: result.pageIndex + 1,
                        overlayText: 'Confidential',
                        fillColor: '#000000',
                        fontColor: '#FFFFFF'
                    } as RedactionSettings);
                    redactionCount++;
                }
            }
            status.textContent = redactionCount
                ? `Marked ${redactionCount} match${redactionCount === 1 ? '' : 'es'} for "${term}". Apply Redactions to make them permanent.`
                : `No matches found for "${term}".`;
        } catch (error) {
            console.error('Unable to search the PDF for redaction text.', error);
            status.textContent = 'Unable to search the PDF. See the browser console for details.';
        } finally {
            isSearching = false;
            updateControls();
        }
    });

    applyButton.addEventListener('click', (): void => {
        if (!window.confirm('Applying redactions permanently removes the marked content. Continue?')) {
            status.textContent = 'Redaction application canceled.';
            return;
        }
        isApplyingRedactions = true;
        viewer.annotation.redact();
        status.textContent = 'Applying redactions. The viewer will reload when processing is complete.';
    });

    viewer.appendTo('#redaction-viewer');
    updateControls();
}

```
