Due to the fact that PDF is one of the most in-use formats of the digital world, it is often required to compare two versions of the same document. This article discusses, how to compare two PDF documents and highlight the differences using C#. Further, we will see how to compare password-protected PDF files, accept and reject changes, and also the comparison of more than two PDF files with C# examples.
The following topics are discussed here:
- PDF Comparison .NET API
- Compare Two PDF Documents
- Accept or Reject Identified Changes within PDF Documents
- Compare More than Two PDF Documents
- Compare Password Protected PDF Files
.NET API to Compare PDF Files
GroupDocs.Comparison for .NET is the API that allows comparison among multiple PDF documents and many other files of the same document format within the .NET applications. I will use this API in C# code examples of this article to compare PDF documents.
You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.
PM> Install-Package GroupDocs.Comparison
Compare PDF Documents using C#
If you have multiple copies of a PDF document, you can compare these files to find the differences (additions, deletions). After comparing the PDF content, you can create a new document that highlights all the identified changes. The following are the steps to compare two PDF documents and highlight the differences using C#.
- 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 C# 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 C#
Just like the track changes features, you can programmatically accept or reject each identified change in PDF documents. The following steps show how to compare and then accept or reject the identified changes within the PDF documents.
- Load the source and target PDF files using Comparer class.
- Compare of loaded documents using Compare() method.
- Get the identified changes using GetChanges() method.
- Now traverse the changes and set the ComparisonAction.
- Select Accept, or Reject for each 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 C#.
Compare More than Two PDF Files using C#
Similarly, you can compare more than two documents. The following are the steps that compare multiple PDF documents for differences and highlight the identified changes.
- Load the first PDF files using Comparer class.
- Add other document(s) to Comparer using the Add() method.
- Compare all the PDF files using the Compare() method and get the changes & summary of changes.
The following example shows how to compare multiple PDF files in C# and get the changes in the resultant document.
Compare Password Protected PDF Documents using C#
You can compare the password-protected files by just providing their passwords while loading these documents. The following steps show how we can compare the PDF content of password-protected documents using C#.
- Prepare the loading options for source and target documents by providing the password.
- Load the source document using Comparer class.
- Add the target document to Comparer using the prepared loading options.
- Get the differences summary by calling the Compare() method.
The following example compares two password-protected PDF files and highlights the identified differences in a separate document using C#.
Get a Free API License
You can get a free temporary license to use the API without the evaluation limitations.
Conclusion
To conclude, we learned how to compare two or more PDF files using C#. Further, we highlighted the differences and programmatically accept or reject the identified changes. Lastly, we saw, how to compare password-protected PDF documents within .NET applications.
Several other customizations are available to control the comparison results. You can set the comparison sensitivity, show only the summary page, ignore gaps, etc. Learn more about GroupDocs.Comparison for .NET from the documentation. You can build your own document comparison applications for various document formats. For queries, contact us via the forum.