# pdf-viewer-semantic-text-comparison

> PDF viewer sample comparing uploaded documents and highlighting text differences.

**Framework:** javascript  **Component:** PdfViewer  **Variant:** semantic-text-comparison

## 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-semantic-text-comparison.json
```

**Install Package(s)**

```bash
npm install @syncfusion/ej2-pdfviewer @syncfusion/ej2-inputs
```

**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-semantic-text-comparison/pdf-viewer.html

```html
<div class="control-section semantic-comparison">
    <div class="comparison-uploaders">
        <div class="comparison-uploader">
            <label for="originalFileUploader">Original document</label>
            <input id="originalFileUploader" type="file" />
            <span id="originalFileName">Choose a PDF file</span>
            <button id="originalRemoveBtn" type="button" aria-label="Remove original PDF" hidden>Remove</button>
        </div>
        <div class="comparison-uploader">
            <label for="modifiedFileUploader">Modified document</label>
            <input id="modifiedFileUploader" type="file" />
            <span id="modifiedFileName">Choose a PDF file</span>
            <button id="modifiedRemoveBtn" type="button" aria-label="Remove modified PDF" hidden>Remove</button>
        </div>
        <button id="compareBtn" class="e-btn e-primary" type="button" disabled>Compare</button>
    </div>
    <div id="comparer-container" style="height:640px; width:100%;"></div>
</div>
<style>
    .semantic-comparison { padding: 8px; }
    .comparison-uploaders { display:flex; align-items:end; gap:16px; flex-wrap:wrap; padding:8px 0 16px; }
    .comparison-uploader { display:flex; align-items:center; gap:8px; min-width:280px; padding:12px; border:1px dashed #c3c3cc; border-radius:6px; }
    .comparison-uploader label { font-weight:600; }
    .comparison-uploader input { max-width:210px; }
    .comparison-uploader span { color:#666; font-size:12px; }
    .comparison-uploader button { border:0; background:transparent; cursor:pointer; }
    .comparison-uploader .e-upload { display:none; }
    @media (max-width: 720px) {
        .comparison-uploader { width:100%; flex-wrap:wrap; }
    }
</style>

```

### src/components/pdf-viewer-semantic-text-comparison/pdf-viewer.ts

```typescript
import { PdfComparer, TextComparisonOptions } from '@syncfusion/ej2-pdfviewer';
import { Uploader } from '@syncfusion/ej2-inputs';
import type { SelectedEventArgs } from '@syncfusion/ej2-inputs';

const defaultOriginalDocument = 'https://cdn.syncfusion.com/content/pdf/original-document.pdf';
const defaultModifiedDocument = 'https://cdn.syncfusion.com/content/pdf/modified-document.pdf';
const resourceUrl = 'https://cdn.syncfusion.com/ej2/35.1.37/dist/ej2-pdfviewer-lib';

export function initPdfViewerSemanticTextComparison(): void {
    let originalFile: File | null = null;
    let modifiedFile: File | null = null;
    let originalFilePath = defaultOriginalDocument;
    let modifiedFilePath = defaultModifiedDocument;
    let comparer: PdfComparer | null = null;
    let originalUploader: Uploader;
    let modifiedUploader: Uploader;

    const originalName = document.getElementById('originalFileName') as HTMLElement;
    const modifiedName = document.getElementById('modifiedFileName') as HTMLElement;
    const originalRemove = document.getElementById('originalRemoveBtn') as HTMLButtonElement;
    const modifiedRemove = document.getElementById('modifiedRemoveBtn') as HTMLButtonElement;
    const compareButton = document.getElementById('compareBtn') as HTMLButtonElement;

    const updateCompareButton = (): void => {
        compareButton.disabled = !originalFile || !modifiedFile;
    };

    const setFile = (file: File, isOriginal: boolean): void => {
        if (!file.name.toLowerCase().endsWith('.pdf')) {
            alert('Please select a valid PDF file.');
            (isOriginal ? originalUploader : modifiedUploader).clearAll();
            return;
        }

        const path = URL.createObjectURL(file);
        if (isOriginal) {
            if (originalFilePath !== defaultOriginalDocument) {
                URL.revokeObjectURL(originalFilePath);
            }
            originalFile = file;
            originalFilePath = path;
            originalName.textContent = `${file.name} (${Math.round(file.size / 1024)} KB)`;
            originalRemove.hidden = false;
        } else {
            if (modifiedFilePath !== defaultModifiedDocument) {
                URL.revokeObjectURL(modifiedFilePath);
            }
            modifiedFile = file;
            modifiedFilePath = path;
            modifiedName.textContent = `${file.name} (${Math.round(file.size / 1024)} KB)`;
            modifiedRemove.hidden = false;
        }
        updateCompareButton();
    };

    const clearFile = (isOriginal: boolean): void => {
        if (isOriginal) {
            if (originalFilePath !== defaultOriginalDocument) {
                URL.revokeObjectURL(originalFilePath);
            }
            originalFile = null;
            originalFilePath = defaultOriginalDocument;
            originalUploader.clearAll();
            originalName.textContent = 'Choose a PDF file';
            originalRemove.hidden = true;
        } else {
            if (modifiedFilePath !== defaultModifiedDocument) {
                URL.revokeObjectURL(modifiedFilePath);
            }
            modifiedFile = null;
            modifiedFilePath = defaultModifiedDocument;
            modifiedUploader.clearAll();
            modifiedName.textContent = 'Choose a PDF file';
            modifiedRemove.hidden = true;
        }
        updateCompareButton();
    };

    originalUploader = new Uploader({
        multiple: false,
        allowedExtensions: '.pdf',
        selected: (args: SelectedEventArgs): void => {
            const file = args.filesData[0]?.rawFile as File | undefined;
            if (file) {
                setFile(file, true);
            }
        }
    });
    modifiedUploader = new Uploader({
        multiple: false,
        allowedExtensions: '.pdf',
        selected: (args: SelectedEventArgs): void => {
            const file = args.filesData[0]?.rawFile as File | undefined;
            if (file) {
                setFile(file, false);
            }
        }
    });
    originalUploader.appendTo('#originalFileUploader');
    modifiedUploader.appendTo('#modifiedFileUploader');
    originalRemove.addEventListener('click', () => clearFile(true));
    modifiedRemove.addEventListener('click', () => clearFile(false));
    compareButton.addEventListener('click', async () => {
        if (!originalFile || !modifiedFile) {
            return;
        }
        comparer?.destroy();
        const options: TextComparisonOptions = {
            beforeColor: '#FF0000',
            afterColor: '#00FF00',
            beforeColorOpacity: 0.4,
            afterColorOpacity: 0.4,
            enableHighlights: true
        } as TextComparisonOptions;
        comparer = new PdfComparer({
            originalDocumentPath: originalFilePath,
            modifiedDocumentPath: modifiedFilePath,
            resourceUrl,
            comparisonOptions: options,
            enableDifferencePanel: true,
            enableSyncScrolling: true
        });
        comparer.appendTo('#comparer-container');
        await comparer.compare();
    });
}

```
