# docx-editor-compare-documents

> Word editor compares uploaded original and revised documents and displays the revision results.

**Framework:** angular  **Component:** DocxEditor  **Variant:** compare-documents

## 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/docx-editor-compare-documents.json
```

**Install Package(s)**

```bash
npm install @syncfusion/ej2-angular-documenteditor @syncfusion/ej2-angular-buttons
```

**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/docx-editor-compare-documents/docx-editor.component.html

```html
<div class="control-section" style="width: 100%;">
  <!-- Shared container -->
  <div style="width: 760px; margin: 10px auto">
    <!-- Upload row -->
    <div style="display: flex; gap: 6px">
      <!-- Original file section-->
      <div style="width: 360px; border-radius: 6px; padding: 14px; border: 1px dashed #ccc">
        <!-- Top row -->
        <div style="display: flex; align-items: center; gap: 10px;">
          <span>Original document</span>
          <label style=" padding: 4px 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            cursor: pointer;">
            Choose file
            <input type="file"
              style="display: none;"
              accept=".docx,.sfdt"
              (change)="onOriginalFileChange($event)" />
          </label>
        </div>         
        <!-- Filename + Delete icon -->
        <div style="display: flex; justify-content: space-between; align-items: center; margin-top: 6px; font-size: 12px; color: #666;">
          <span>{{ getOriginalFileName() }} </span>
          <span *ngIf="originalFile" class="e-icons e-trash" (click)="removeOriginalFile()" style="cursor: pointer;" title="Remove file"></span>
        </div>
      </div>
      <!-- Revised file section-->
      <div style="width: 360px; border-radius: 6px; padding: 14px; border: 1px dashed #ccc;">
        <!-- Top row -->
        <div style="display: flex; align-items: center; gap: 10px;">
          <span>Revised document</span>
          <label style="
            padding: 4px 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            cursor: pointer;">
            Choose file
            <input type="file"
              style="display: none;"
              accept=".docx,.sfdt"
              (change)="onRevisedFileChange($event)" />
          </label>
        </div>
        <!-- Filename + Delete icon -->
        <div style="display: flex; justify-content: space-between; align-items: center; margin-top: 6px; font-size: 12px; color: #666;">
          <span>{{ getRevisedFileName() }} </span>
          <span *ngIf="revisedFile" class="e-icons e-trash" (click)="removeRevisedFile()" style="cursor: pointer;" title="Remove file"></span>
        </div>
      </div>
    </div>
    <!-- Checkbox, Compare & Download buttons  -->
    <div style="display: flex; justify-content: space-between; align-items: center; margin-top: 12px;">  
      <!-- Checkbox -->
      <div>
        <label style="display: inline-flex; align-items: center; cursor: pointer;">
          <input
            type="checkbox"
            [checked]="showResult"
            (change)="onShowResultChange($event)"
            aria-label="Show comparison results with tracked changes"
            style="margin-right: 6px;" />
          <span>Show comparison results with tracked changes</span>
        </label>
      </div>
      <!-- Buttons -->
      <div style="display: flex; gap: 10px; margin-right: 32px;">
        <button ejs-button
          class="e-primary"        
          [disabled]="!originalFile || !revisedFile"
          (click)="onCompare()">
          Compare
        </button>
        <button ejs-button
          class="e-outline e-flat e-primary"
          (click)="onDownload()"
          iconCss="e-icons e-download"
          [disabled]="!compareClicked">
          Download Result Document
        </button>
      </div>
    </div>
  </div>

  <!-- Editors Area -->
  <div style="display: flex; gap: 6px; height: calc(100vh - 330px); padding: 0px 10px;">
    <!-- Left Container -->
    <div style="width: 50%; display: flex; flex-direction: column; border: 1px solid #ddd; overflow: hidden;">
      <div style="padding: 8px; font-weight: 600;" class="e-de-ctn-title">Original Document</div>
      <div style="flex: 1px; display:block; height: calc(100vh - 330px);">
        <ejs-documenteditorcontainer
          #editor1
          [serviceUrl]="serviceUrl"
          [height]="'100%'"
          [width]="'100%'"
          [enableToolbar]="false"
          [showPropertiesPane]="false"
        ></ejs-documenteditorcontainer>
      </div>
    </div>    
    <!-- Right Editor -->
    <div style="width: 50%; display: flex; flex-direction: column; border: 1px solid #ddd; overflow: hidden;">
      <div class="e-de-ctn-title" style="display: flex; justify-content: space-between; align-items: center">
        <div style="float: left; padding: 8px; font-weight: 600; flex: 1px;" >
          {{
            showResult
              ? 'Result Document (with tracked changes)'
              : 'Revised Document'
          }}
        </div>
        <button ejs-button
            *ngIf="showResult"
            class="e-de-ctn-title"
            iconCss="e-icons e-eye"
            style="float: right; font-weight: 400; background: transparent; box-shadow: none; border-color: transparent; border-radius: 0px;
                   color: inherit; font-size: 12px;"
            title="Show/Hide Revisions Pane"
            (click)="OnToggleRevisionPane()">
            {{showRevisions ? ' Hide Review Pane' : ' Show Review Pane'}}
        </button>
      </div>      
      <div style="flex: 1px; display:block; height: calc(100vh - 330px);">
        <ejs-documenteditorcontainer
          #editor2
          [serviceUrl]="serviceUrl"
          [height]="'100%'"
          [width]="'100%'"
          [enableToolbar]="false"
          [showPropertiesPane]="false"
        ></ejs-documenteditorcontainer>
      </div>
    </div>
  </div>

  <!-- Waiting Popup Overlay -->
  <div class="overlay" id="popup-overlay" style="display: none;"></div>
  <div id='waiting-popup' style="display: none;">
    <svg class="circular" height="40" width="40">
      <circle class="circle-path" cx="25" cy="25" r="20" fill="none" stroke-width="6" stroke-miterlimit="10" />
    </svg>
  </div>
</div>

<style>
    
  .overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(56, 56, 56, 0.5);
    z-index: 9999;
    display: none;
    cursor: not-allowed;
  }

  #waiting-popup {
    width: 56px;
    height: 56px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    padding: 3px;
    background: white;
    box-shadow: 0px 3px 6px rgba(0,0,0,.3);
    z-index: 10000;
    display: none;
  }

  .circular {
      animation: rotate 2s linear infinite;
      height: 50px;
      width: 50px;
      border-radius: 50%;
  }

  .circle-path {
      stroke-dasharray: 1, 200;
      stroke-dashoffset: 0;
      animation: dash 1.5s ease-in-out infinite, color 6s ease-in-out infinite;
      stroke-linecap: round;
      stroke: #ff4081;
  }

  @keyframes rotate {
      100% {
          transform: rotate(360deg);
      }
  }

  @keyframes dash {
      0% {
          stroke-dasharray: 1, 200;
          stroke-dashoffset: 0;
      }

      50% {
          stroke-dasharray: 89, 200;
          stroke-dashoffset: -35;
      }

      100% {
          stroke-dasharray: 89, 200;
          stroke-dashoffset: -124;
      }
  }
</style>  

```

### src/components/docx-editor-compare-documents/docx-editor.component.ts

```typescript
import { NgIf } from '@angular/common';
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import { ToolbarService, SelectionService, DocumentEditorContainerComponent, DocumentEditorContainerModule } from '@syncfusion/ej2-angular-documenteditor';
import { ButtonModule} from '@syncfusion/ej2-angular-buttons';
import { originalDocument, revisedDocument } from '../datasource/docx-editor-data';

@Component({
    selector: 'app-root',
    templateUrl: 'docx-editor.component.html',
    encapsulation: ViewEncapsulation.None,
    providers: [ToolbarService, SelectionService],
    standalone: true,
    imports: [NgIf, DocumentEditorContainerModule, ButtonModule]
})

export class CompareDocumentsComponent {
  @ViewChild('editor1', { static: false }) editorRef1?: DocumentEditorContainerComponent;
  @ViewChild('editor2', { static: false }) editorRef2?: DocumentEditorContainerComponent;

  serviceUrl: string = 'https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/';
  originalFile: File | null = null;
  revisedFile: File | null = null;
  showResult: boolean = true;
  compareClicked: boolean = false;
  showRevisions: boolean = false;

  ngAfterViewInit() {
    setTimeout(() => {
      const ed1 = this.editorRef1?.documentEditor;
      const ed2 = this.editorRef2?.documentEditor;
      if (ed1 && ed2) {
        ed1.open(JSON.stringify(originalDocument));
        ed2.showRevisions = false;
        ed2.open(JSON.stringify(revisedDocument));
        ed1.viewChange = () => {
          const pos = ed1.selection.getScrollPosition();
          ed2.selection.setScrollPosition(pos);
        };
        ed2.viewChange = () => {
          const pos = ed2.selection.getScrollPosition();
          ed1.selection.setScrollPosition(pos);
        };
      }
    });
  }

  isSupportedFormatType(formatType: string): boolean {
    switch (formatType) {
      case '.docx':
      case '.sfdt':
        return true;
      default:
        return false;
    }
  }

  async openFileInEditor(file: File, editorRef?: DocumentEditorContainerComponent): Promise<void> {
    const formatType = file.name.slice(file.name.lastIndexOf('.')).toLowerCase();
    if(editorRef){
      if (formatType === '.sfdt') {
        const docData = await file.text();
        editorRef.documentEditor.showRevisions = false;
        editorRef.documentEditor.open(docData);
      } else if (this.isSupportedFormatType(formatType)) {
        const formData = new FormData();
        formData.append('file', file);
        try {
          const response = await fetch(this.serviceUrl + 'Import', {
            method: 'POST',
            body: formData,
          });
          const sfdtString = await response.text();
          let sfdtObject: any = null;
          try {
            sfdtObject = JSON.parse(sfdtString);
          } catch (e) {
            alert("Unable to display the compared document. Please try again.");
          }
          if (sfdtObject && editorRef?.documentEditor) {
            editorRef.documentEditor.showRevisions = false;
            editorRef.documentEditor.open(JSON.stringify(sfdtObject));
          } else {
            alert("Unable to display the compared document. Please try again.");
          }
        } catch (e) {
          alert('This Compare Documents demo supports only DOCX and SFDT file formats. Please select a valid DOCX or SFDT document and try again.');
        }
      } else {
        alert('Unsupported file type. Please use either .docx or .sfdt file.');
        this.compareClicked = false;
      }
    }
  }

  async loadComparedDocumentAndOpen(editorRef2: DocumentEditorContainerComponent, originalFile: File, revisedFile: File): Promise<void> {
    const formData = new FormData();
    formData.append('originalFile', originalFile);
    formData.append('revisedFile', revisedFile);
    formData.append("author", "Author");
    formData.append("dateTime", new Date().toISOString());
    try {
      const response = await fetch(this.serviceUrl + 'CompareDocuments', {
        method: 'POST',
        body: formData,
      });
      if (!response.ok) {
        alert("Failed to compare the selected documents. Please try again."); 
        return;
      }
      const sfdtString = await response.text();
      let sfdtObject: any = null;
      try {
        sfdtObject = JSON.parse(sfdtString);
      } catch {
        alert("The comparison could not be completed due to no valid JSON. Please try again.");
        return;
      }
      if (sfdtObject && editorRef2?.documentEditor) {
        editorRef2.documentEditor.showRevisions = false;
        editorRef2.documentEditor.open(JSON.stringify(sfdtObject));
      } else {
        alert("Unable to display the comparison result. Please try again.");
      }
    } catch (e) {
      alert('Error on comparing documents: ' + e);
    }
  }

  removeOriginalFile(): void {
      this.originalFile = null;
      this.compareClicked = false;
  }

  removeRevisedFile(): void {
      this.revisedFile = null;
      this.compareClicked = false;
  }

  getOriginalFileName(): string {
    if (!this.originalFile) {
      return 'Supported formats: SFDT, DOCX';
    }

    const size = Math.round(this.originalFile.size / 1024);
    return `${this.originalFile.name} (${size} KB)`;
 }

 getRevisedFileName(): string {
    if (!this.revisedFile) {
      return 'Supported formats: SFDT, DOCX';
    }

    const size = Math.round(this.revisedFile.size / 1024);
    return `${this.revisedFile.name} (${size} KB)`;
 }

  onOriginalFileChange(event: any) {
    this.originalFile = event.target.files && event.target.files[0] ? event.target.files[0] : null;
    this.compareClicked = false;
  }
  onRevisedFileChange(event: any) {
    this.revisedFile = event.target.files && event.target.files[0] ? event.target.files[0] : null;
    this.compareClicked = false;
  }
  onShowResultChange(event: any) {
    this.showResult = event.target.checked;
  }

  showHideWaitingIndicator(show: boolean): void {
    let waitingPopUp: HTMLElement | null = document.getElementById('waiting-popup');
    let inActiveDiv: HTMLElement | null = document.getElementById('popup-overlay');
    if (waitingPopUp && inActiveDiv){
    inActiveDiv.style.display = show ? 'block' : 'none';
    waitingPopUp.style.display = show ? 'block' : 'none';
    }
  }

  async onCompare(): Promise<void> {    
    this.compareClicked = false;
    this.showRevisions = false;
    setTimeout(async () => {
      if (this.originalFile && this.revisedFile){
        var orginalFileFormatType = this.originalFile.name.slice(this.originalFile.name.lastIndexOf('.')).toLowerCase();
        var revisedFileFormatType = this.revisedFile.name.slice(this.revisedFile.name.lastIndexOf('.')).toLowerCase();
        if(this.isSupportedFormatType(orginalFileFormatType) && this.isSupportedFormatType(revisedFileFormatType)){
          this.showHideWaitingIndicator(true);   
          this.compareClicked = true;
          if (this.showResult) {
            if (this.editorRef1)
                await this.openFileInEditor(this.originalFile, this.editorRef1);                       
            if (this.editorRef2)
                await this.loadComparedDocumentAndOpen(this.editorRef2, this.originalFile, this.revisedFile);           
          } else {
            if (this.editorRef1)
              await this.openFileInEditor(this.originalFile, this.editorRef1);
            if (this.editorRef2)
              await this.openFileInEditor(this.revisedFile, this.editorRef2);
          }
          this.showHideWaitingIndicator(false);
        }
        else{          
          alert('Unsupported file type is selected. Please use either .docx or .sfdt file.');
        }
      }
    }, 0);
  }

  onDownload(): void {
    if (this.editorRef2?.documentEditor)
      this.editorRef2.documentEditor.save('Result', 'Docx');
  }

  OnToggleRevisionPane(): void {
    this.showRevisions = !this.showRevisions;
     const editor2 = this.editorRef2?.documentEditor;
      if (editor2) {
        editor2.showRevisions = this.showRevisions;
      }
  }

}
```
