# pdf-viewer-redaction

> PDF viewer supports redaction annotations added programmatically or by searching for text, with one confirmed action to apply them permanently.

**Framework:** react  **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/react/ts/pdf-viewer-redaction.json
```

**Install Package(s)**

```bash
npm install @syncfusion/ej2-react-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/PdfViewer.tsx

```tsx
import { useRef, useState } from 'react';
import {
    Annotation, BookmarkView, FormDesigner, FormFields, Inject, LinkAnnotation, Magnification,
    Navigation, PageOrganizer, PdfViewerComponent, Print, RedactionSettings, TextSearch,
    TextSelection, ThumbnailView, Toolbar
} from '@syncfusion/ej2-react-pdfviewer';

function RedactionSample() {
    const viewerRef = useRef<PdfViewerComponent | null>(null);
    const isApplyingRedactions = useRef(false);
    const [isDocumentLoaded, setIsDocumentLoaded] = useState(false);
    const [searchText, setSearchText] = useState('');
    const [isSearching, setIsSearching] = useState(false);
    const [statusMessage, setStatusMessage] = useState('Loading PDF document...');

    const onDocumentLoad = () => {
        viewerRef.current?.toolbar.showRedactionToolbar(true);
        setIsDocumentLoaded(true);
        if (isApplyingRedactions.current) {
            isApplyingRedactions.current = false;
            setStatusMessage('Redactions applied to the document.');
        } else {
            setStatusMessage('Document loaded. Add a redaction or search for text to mark.');
        }
    };

    const addRedaction = () => {
        const viewer = viewerRef.current;
        if (!viewer) {
            return;
        }

        viewer.annotation.addAnnotation('Redaction', {
            bound: { x: 150, y: 400, width: 200, height: 40 },
            pageNumber: viewer.currentPageNumber,
            overlayText: 'Confidential',
            fillColor: '#000000',
            fontColor: '#FFFFFF'
        } as RedactionSettings);
        setStatusMessage('Redaction added to the current page. Apply Redactions to make it permanent.');
    };

    const searchAndMarkRedactions = async () => {
        const viewer = viewerRef.current;
        const term = searchText.trim();
        if (!viewer) {
            return;
        }
        if (!term) {
            setStatusMessage('Enter text to search for and redact.');
            return;
        }

        setIsSearching(true);
        setStatusMessage(`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++;
                }
            }

            setStatusMessage(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);
            setStatusMessage('Unable to search the PDF. See the browser console for details.');
        } finally {
            setIsSearching(false);
        }
    };

    const applyRedactions = () => {
        const viewer = viewerRef.current;
        if (!viewer) {
            return;
        }
        if (!window.confirm('Applying redactions permanently removes the marked content. Continue?')) {
            setStatusMessage('Redaction application canceled.');
            return;
        }

        isApplyingRedactions.current = true;
        viewer.annotation.redact();
        setStatusMessage('Applying redactions. The viewer will reload when processing is complete.');
    };

    return (
        <div className='control-section'>
            <div
                role='group'
                aria-label='Redaction controls'
                style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'flex-end', gap: '12px', marginBottom: '12px' }}
            >
                <button
                    className='e-btn e-primary'
                    type='button'
                    onClick={addRedaction}
                    disabled={!isDocumentLoaded}
                >
                    Add Redaction
                </button>
                <div style={{ width: '220px' }}>
                    <label htmlFor='redaction-search-text' style={{ display: 'block', marginBottom: '4px' }}>
                        Text to redact
                    </label>
                    <input
                        id='redaction-search-text'
                        className='e-input'
                        type='search'
                        value={searchText}
                        onChange={(event) => setSearchText(event.target.value)}
                        disabled={!isDocumentLoaded || isSearching}
                    />
                </div>
                <button
                    className='e-btn'
                    type='button'
                    onClick={searchAndMarkRedactions}
                    disabled={!isDocumentLoaded || isSearching}
                >
                    {isSearching ? 'Searching...' : 'Search & Mark for Redaction'}
                </button>
                <button
                    className='e-btn e-danger'
                    type='button'
                    onClick={applyRedactions}
                    disabled={!isDocumentLoaded}
                >
                    Apply Redactions
                </button>
            </div>
            <p role='status'>{statusMessage}</p>
            <PdfViewerComponent
                id='redaction-viewer'
                ref={viewerRef}
                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'
                    ]
                }}
                documentLoad={onDocumentLoad}
                style={{ height: '640px' }}
            >
                <Inject services={[
                    Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,
                    Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner, PageOrganizer
                ]} />
            </PdfViewerComponent>
        </div>
    );
}

export default RedactionSample;

```
