How to Compare Text, Word, PDF with Java or Kotlin
How to Compare Text, Word, PDF with Java or Kotlin
Leave feedback
On this page
After going through this article, you will be able to compare text files, Word files, PDF files, and other documents in Java-based applications. By using this feature, we can compare invoices, contracts, presentations, AutoCAD designs, price lists, or programming files. We will also have opportunity to highlight the identified changes and have the option to either accept or reject any change. You can even build our own document comparison tool similar to the one launched by GroupDocs, using the document comparison API for Java.
Compare Word Files and Show Differences using Java or Kotlin
Steps below will show you how to compare any two Word documents in just a few lines of Java or Kotlin code. As a result, you will get the resultant document that will be highlighting the identified changes.
Initialize the Comparer object with the source document path.
Add the second document to compare using the add method of Comparer object.
Call the compare method to get the result of the comparison. The compare method takes the name of the output document as a parameter.
The code below shows how to compare two Word documents in Java
JavaKotlin
Here is an example of the resultant Word document generated by the code above. It contains the highlighted differences of the compared documents. The deleted content will be marked in Red, added content will be displayed in Blue, however, Green shows the modified content.
Comparing Word files using Java or Kotlin streams
You can similarly pass the document as a stream to the Comparer class to get it compared with the second document.
Here is the Java/Kotlin code to give you a clear idea
JavaKotlin
Using streams can be useful to compare documents without saving them on a disk
Accepting or rejecting changes in Word file using Java or Kotlin
After comparing documents, you have the option to either accept or reject any change. Below you can see the example of accepting and rejecting the changes alternatively. You may display each change one by one with the similar code and take your decisions to accept/reject each change according to your requirement.
Here is the Java/Kotlin code to give you a clear idea
JavaKotlin
More details about accepting and rejecting changes can be found in documentation
Compare text files and show differences using Java or Kotlin
Using the Comparer class, we can also compare any text files. Below is the similar code for comparing two text files in Java or Kotlin. Steps are exactly the same as comparing any other two documents:
Initialize the Comparer object with the source text file.
Add the second file using the add method of Comparer object.
Call the compare method to get the result of the comparison.
The code below shows how to compare two Text files in Java or Kotlin
JavaKotlin
Here is the source, target and output text files that shows the comparison result of matching two text files using the code above.
Compare PDF Files for Text Difference using Java or Kotlin
We can compare PDF files using the same code, just by changing the file extensions to .pdf.
The code below compares two pdf files and shows differences
JavaKotlin
Here is a result of comparing two PDF files in Java or Kotlin.
Many other open-source examples are available at GitHub Repository. You may download and quickly run examples using the getting started guide. In case of any questions, look at the documentation or reach us at any time on the forum.