How to compare files in Java or Kotlin

The GroupDocs.Comparison is a Java library which allows you to compare documents and see differences. Compare Microsoft Word, Excel, PowerPoint, OpenDocument, PDF, Text, HTML and many other documents, retrieve a list of changes between source and target documents, apply or reject changes and save results with GroupDocs.Comparison API. In addition to this, GroupDocs.Comparison can identify styling and formatting changes – like bold, italic, underlines, strikethroughs, font types, etc.

Changes detection algorithms used by GroupDocs.Comparison allows you to detect differences in various document parts and blocks:

  • Text blocks – paragraphs, words and characters
  • Tables
  • Images
  • Shapes etc.

Here are simple steps to compare two text files and show differences:

  • Initialize the Comparer object with the source document.
  • Add the second file using the add method of Comparer object.
  • Call the compare method to get the result of the comparison.

The following code snippet demonstrates the simplest case of documents comparison using a couple lines of code.

Compare documents using local files

You can also use streams to compare documents. Not only file stream, there is no problem in comparing files from internet without saving them locally, or compare files on-fly.

Compare documents using streams

Let’s say you have two contracts in DOCX format that were concluded in different years. If you use the above code to compare these contracts, you get a DOCX file where deleted elements are marked in red, added in blue, and modified in green as shown below:

Comparing two contracts

Accept or Reject detected differences

GroupDocs.Comparison provides an ability to apply or discard specific changes between source and target documents and save the resultant document with (or without) selected changes.

The following are the steps to apply/reject changes to the resultant document.

The following code sample shows how to accept/reject detected differences.

Generate document pages preview

GroupDocs.Comparison allows you generate page’s previews for source, target and resultant documents using generatePreview method of a Document class. Class PreviewOptions is used to manage preview generation process – specify page numbers, image format etc.

The following steps shows how to generate a document preview with GroupDocs.Comparison for Java API:

  • Create a new instance of Comparer class and pass the source document path as a constructor parameter.
  • Add target document(s) to comparer object using add method.
  • getSource and getTargets methods of Comparer object allows to access source and target documents and provides GeneratePreview method
  • Instantiate the PreviewOptions object with:
    • callback for each page stream creation (see event handler CreatePageStream);
    • image preview format – PNG / JPG / BMP;
    • page numbers to process;
    • custom size of preview images (if needed).
  • Call generatePreview method of dource and targets Document objects passing PreviewOptions to it.

Get page previews for resultant document

Compare multiple documents

GroupDocs.Comparison allows comparing more than two documents. The following code sample shows how to compare multiple documents programmatically.

How to compare multiple documents in Java


Maven is the easiest way to download and install GroupDocs.Comparison for Java. Please get a temporary license to test the library without any functional restrictions.

Please check the documentation to learn more about the library. We also offer free technical support so please feel free to contact us – we will be happy to help.

See also