# pdf-viewer-semantic-text-comparison

> Compare two PDFs with synchronized scrolling, highlighted text changes, and a difference panel.

**Framework:** angular  **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/angular/pdf-viewer-semantic-text-comparison.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-semantic-text-comparison/pdf-viewer.component.html

```html
<div class="control-section">
    <div role="group" aria-label="PDF comparison files"
        style="display:flex;align-items:flex-end;flex-wrap:wrap;gap:12px;margin-bottom:12px">
        <div style="min-width:260px;flex:1 1 260px">
            <label for="original-pdf-file" style="display:block;margin-bottom:4px">Original document</label>
            <input id="original-pdf-file" class="e-input" type="file" accept=".pdf,application/pdf"
                (change)="onOriginalFileChange($event)">
            <div style="margin-top:4px">{{ originalFile?.name ?? 'Choose a PDF file' }}</div>
        </div>
        <div style="min-width:260px;flex:1 1 260px">
            <label for="modified-pdf-file" style="display:block;margin-bottom:4px">Modified document</label>
            <input id="modified-pdf-file" class="e-input" type="file" accept=".pdf,application/pdf"
                (change)="onModifiedFileChange($event)">
            <div style="margin-top:4px">{{ modifiedFile?.name ?? 'Choose a PDF file' }}</div>
        </div>
        <button class="e-btn e-primary" type="button" (click)="compareDocuments()"
            [disabled]="!originalFile || !modifiedFile || isComparing">
            {{ isComparing ? 'Comparing...' : 'Compare' }}
        </button>
    </div>
    <p role="status" aria-live="polite">{{ statusMessage }}</p>
    <div #comparerContainer [hidden]="!comparisonVisible" style="height:640px;width:100%"></div>
</div>

```

### src/components/pdf-viewer-semantic-text-comparison/pdf-viewer.component.ts

```typescript
import { ChangeDetectorRef, Component, ElementRef, OnDestroy, ViewChild } from '@angular/core';
import { PdfComparer, TextComparisonOptions } from '@syncfusion/ej2-pdfviewer';

interface ComparisonDocuments {
    original: string;
    modified: string;
}

@Component({
    selector: 'control-content',
    templateUrl: 'pdf-viewer.component.html',
    standalone: true
})
export class SemanticTextComparisonComponent implements OnDestroy {
    @ViewChild('comparerContainer', { static: true })
    private comparerContainer!: ElementRef<HTMLDivElement>;

    public readonly resource = 'https://cdn.syncfusion.com/ej2/35.1.37/dist/ej2-pdfviewer-lib';
    public originalFile: File | null = null;
    public modifiedFile: File | null = null;
    public comparisonVisible = false;
    public isComparing = false;
    public statusMessage = 'Choose two PDF files to compare.';
    private comparer: PdfComparer | null = null;
    private comparisonDocuments: ComparisonDocuments | null = null;

    private readonly comparisonOptions = {
        beforeColor: '#FF0000',
        afterColor: '#00FF00',
        beforeColorOpacity: 0.4,
        afterColorOpacity: 0.4,
        enableHighlights: true
    } as TextComparisonOptions;

    constructor(private readonly changeDetector: ChangeDetectorRef) {}

    public onOriginalFileChange(event: Event): void {
        this.resetComparison();
        this.originalFile = this.getPdfFile(event, 'Original');
        if (this.originalFile) {
            this.statusMessage = 'PDF selected. Choose the other document or compare the selected files.';
        }
    }

    public onModifiedFileChange(event: Event): void {
        this.resetComparison();
        this.modifiedFile = this.getPdfFile(event, 'Modified');
        if (this.modifiedFile) {
            this.statusMessage = 'PDF selected. Choose the other document or compare the selected files.';
        }
    }

    public async compareDocuments(): Promise<void> {
        if (!this.originalFile || !this.modifiedFile) {
            return;
        }

        this.resetComparison();
        this.isComparing = true;
        this.statusMessage = 'Loading PDF documents for comparison...';
        try {
            this.comparisonDocuments = {
                original: URL.createObjectURL(this.originalFile),
                modified: URL.createObjectURL(this.modifiedFile)
            };
            this.comparisonVisible = true;
            this.changeDetector.detectChanges();

            this.comparer = new PdfComparer({
                originalDocumentPath: this.comparisonDocuments.original,
                modifiedDocumentPath: this.comparisonDocuments.modified,
                resourceUrl: this.resource,
                comparisonOptions: this.comparisonOptions,
                enableDifferencePanel: true,
                enableSyncScrolling: true,
                height: '640px',
                width: '100%'
            });
            this.comparer.appendTo(this.comparerContainer.nativeElement);
            await this.comparer.compare();
            this.statusMessage = 'Comparison requested. Review the highlighted changes in both viewers and the difference panel.';
        } catch (error) {
            console.error('Unable to compare the selected PDF documents.', error);
            this.statusMessage = 'Unable to compare the selected PDFs. See the browser console for details.';
            this.disposeComparer();
        } finally {
            this.isComparing = false;
        }
    }

    public ngOnDestroy(): void {
        this.disposeComparer();
    }

    private getPdfFile(event: Event, label: string): File | null {
        const input = event.currentTarget as HTMLInputElement;
        const file = input.files?.[0] ?? null;
        if (file && !file.name.toLowerCase().endsWith('.pdf')) {
            this.statusMessage = `${label} file must have a .pdf extension.`;
            input.value = '';
            return null;
        }
        return file;
    }

    private resetComparison(): void {
        this.disposeComparer();
        this.comparisonVisible = false;
        this.isComparing = false;
        this.statusMessage = 'PDF selected. Choose the other document or compare the selected files.';
    }

    private disposeComparer(): void {
        this.comparer?.destroy();
        this.comparer = null;
        if (this.comparisonDocuments) {
            URL.revokeObjectURL(this.comparisonDocuments.original);
            URL.revokeObjectURL(this.comparisonDocuments.modified);
            this.comparisonDocuments = null;
        }
    }
}

```
