Comparing PDF files can be a tedious task, especially when you need to identify differences between multiple documents. Fortunately, GroupDocs.Comparison for Java API provides you the power to compare PDF files programmatically. In this article, we’ll show you step-by-step how to compare two PDF files using Java code, including how to handle password-protected PDFs. We’ll also demonstrate how to compare more than two PDF files and how to either accept or reject any of the identified changes. So if you’re ready to streamline your PDF comparison workflow, read on!

Compare PDF Documents to find differences using Java API

The following topics are discussed here:

If you are interested in Comparing PDF Files Online, visit the Online PDF Comparison Tool that is powered by GroupDocs.

Java API to Compare PDF Files

In this article, I will utilize the GroupDocs.Comparison for Java API to compare PDF documents, as this API enables comparison among multiple PDF documents. Additionally, it allows the comparison of various other files of the same document format within Java applications.

You can download the JAR file from the downloads section or use the latest repository and dependency Maven configurations within your Java applications.

<repository>
    <id>groupdocs-artifacts-repository</id>
    <name>GroupDocs Artifacts Repository</name>
    <url>https://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-comparison</artifactId>
    <version>23.3</version>
</dependency>

Compare PDF Documents using Java

To identify differences (additions, deletions) among multiple copies of a PDF document, you can compare these files. Once you’ve compared the PDF content, you can generate a new document that highlights all identified changes. The following steps demonstrate how to use Java to compare two PDF documents and highlight any differences.

  • Load the first PDF document using Comparer class.
  • Add the second file to Comparer using add method.
  • Compare both the PDF files and get the changes summary by calling compare method.

The following Java code snippet shows how to compare PDF documents and highlight the changes in the resultant document.

Accept or Reject Identified Changes of PDF Files using Java

Quite similar to the functionality of “track changes,” it is also possible to accept or reject each detected change in PDF documents through programming. The steps below demonstrate how to compare and then accept or reject the changes that have been identified within the PDF documents.

  • Load the source and target PDF files using Comparer class.
  • Compare the loaded documents using compare method.
  • Get the identified changes using getChanges method.
  • Now traverse the changes and set the appropriate ComparisonAction.
    • Select Accept to adopt the change
    • Select Reject to deny the change.
  • Call the applyChanges method to get the resultant document with the accepted changes.

The following code snippet compares two PDF documents and then accepts an identified change and then rejects another one using Java.

Compare Password Protected PDF Documents using Java

It is possible to compare password-protected files by providing their passwords while loading the documents. The following steps demonstrate how to compare the content of password-protected PDF documents using Java:

  • Prepare loading options for both the source and target documents, providing the necessary passwords.
  • Load the source document using the Comparer class.
  • Add the target document to the Comparer, using the prepared loading options.
  • Obtain a summary of the differences by calling the compare method.

The Java example below compares two password-protected PDF files and highlights the identified differences in a separate document.

Compare More than Two PDF Files using Java

In the same manner, it is also possible to compare more than two documents for differences. The following steps show how to compare multiple PDF documents and highlight the identified changes:

  • Load the first PDF file using the Comparer class.
  • Utilize the add method to add any additional documents to be compared to the Comparer.
  • Compare all subsequent PDF files using the compare method and obtain the changes and summary of differences.

The following example shows how to compare multiple PDF files in Java and get the changes in the resultant document.

Get a Free API License

You can get a free temporary license to use the API without the evaluation limitations.

Conclusion

In summary, we covered comparing two or more PDF files using Java, highlighting the differences, and programmatically accepting or rejecting identified changes. Additionally, we explored how to compare password-protected PDF documents within Java applications.

Numerous other customizations are in your control to manage comparison outcomes. You can adjust comparison sensitivity, choose to display only the summary page, ignore gaps, and more.

Build Your Own PDF Comparison Application

Here are some important links that will help you in building your own document comparison application.

Documentation | API Reference | Supported File Formats

Running Examples – GitHub | Free Support Forum | Release Notes

See Also