Compare documents

GroupDocs.Comparison detects changes in text (paragraphs, words, characters), tables, images, and shapes across a wide range of formats — Word, PDF, Excel, PowerPoint, HTML, AutoCAD, Visio, Outlook, OpenDocument, images, and more. The full list is available on the Supported document formats page.

By default, GroupDocs.Comparison highlights detected changes with the following colours:

  • Inserted – blue
  • Deleted – red
  • Style changed – green

These are the defaults — you can override colours, fonts, and other styling via the InsertedItemStyle, DeletedItemStyle, and ChangedItemStyle properties. See Customize changes styles for details.

Basic comparison workflow

To compare two documents, follow these steps:

  1. Instantiate the Comparer object with source document path or stream.
  2. Call the add() method and specify target document path or stream.
  3. Call the compare() method. It returns the path of the result document.

Compare local documents

import com.groupdocs.comparison.Comparer;
import java.nio.file.Path;
// ...

try (Comparer comparer = new Comparer("source.pdf")) {
    comparer.add("target.pdf");
    final Path resultPath = comparer.compare("result.pdf");
}

The output file is as follows:

Compare documents from stream

import com.groupdocs.comparison.Comparer;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
// ...

try (InputStream sourceInputStream = new FileInputStream("source.docx");
     InputStream targetInputStream = new FileInputStream("target.docx");
     OutputStream resultOutputStream = new FileOutputStream("result.docx");
     Comparer comparer = new Comparer(sourceInputStream)) {
    comparer.add(targetInputStream);
    comparer.compare(resultOutputStream);
}

Customize comparison with options

To control how the comparison is performed and how the result is rendered, pass a CompareOptions object to the compare() method. These options work with any supported document format:

import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.options.CompareOptions;
// ...

try (Comparer comparer = new Comparer("source.docx")) {
    comparer.add("target.docx");

    CompareOptions options = new CompareOptions();
    options.setDetectStyleChanges(true);
    options.setGenerateSummaryPage(true);
    options.setShowDeletedContent(true);
    options.setShowInsertedContent(true);

    comparer.compare("result.docx", options);
}

For format-specific behaviour, GroupDocs.Comparison provides dedicated subclasses of CompareOptions:

Using a format-specific subclass is recommended when comparing a known document type — it makes Word-only or PDF-only settings discoverable and prevents you from passing irrelevant options.

The example below compares only the first two pages of two PDF documents and lays the result out side by side:

import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.options.PagesSetup;
import com.groupdocs.comparison.options.PdfCompareOptions;
// ...

try (Comparer comparer = new Comparer("source.pdf")) {
    comparer.add("target.pdf");

    PagesSetup pagesSetup = new PagesSetup();
    pagesSetup.setStartPage(1); // 1-based, inclusive; null means "from the first page"
    pagesSetup.setEndPage(2);   // 1-based, inclusive; null means "to the last page"

    PdfCompareOptions options = new PdfCompareOptions();
    options.setDisplayMode(PdfCompareOptions.ComparisonDisplayMode.SIDE_BY_SIDE);
    options.setPagesSetup(pagesSetup);

    comparer.compare("result.pdf", options);
}

Configure loading with LoadOptions

While CompareOptions controls how documents are compared, LoadOptions controls how the source and target files are loaded into the Comparer. It is passed to the Comparer constructor and to add(), before any comparison runs.

import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.options.load.LoadOptions;
import com.groupdocs.comparison.result.FileType;
// ...

LoadOptions loadOptions = new LoadOptions();
loadOptions.setFileType(FileType.DOCX);

try (Comparer comparer = new Comparer("source.docx", loadOptions)) {
    comparer.add("target.docx", loadOptions);
    comparer.compare("result.docx");
}

Common LoadOptions properties:

Next steps

Once the basic comparison works, common follow-up tasks include:

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.