Compare PDF Files and Highlight Differences using C#

In certain cases, you may need to compare two or more PDF documents in your .NET application. You can easily compare and highlight the differences programmatically. In this article, you will learn how to compare two or more PDF files and highlight differences using C#.

The following topics are discussed/covered in this article:

C# API to Compare PDF Documents

To compare two PDF files for differences, I will be using GroupDocs.Comparison for .NET API. It compares to detect content changes for words, paragraphs, and characters while providing a comparison document that lists a summary of differences. The .NET compare library supports checking differences in both, content as well as the text style of popular image and document formats such as PDF, HTML, Outlook email, Microsoft Office Word documents, Excel spreadsheets, PowerPoint presentations, OneNote, Visio diagrams, text, and images. It can be used to develop applications in any development environment that targets the .NET platform.

You can either download the DLL of the API or install it using NuGet.

Install-Package GroupDocs.Comparison

Compare Two PDF Files using C#

You can compare two PDF documents by following the simple steps given below:

  1. Create an instance of the Comparer class
  2. Provide the source PDF file path to the constructor
  3. Add target PDF file to the comparison
  4. Call the Compare method along with the output file path

The following code sample shows how to compare two PDF documents and highlight the differences using C#.

Compare Two PDF Files using C#

Compare Two PDF Files using C#

The resulting document includes a summary page at the end of the document showing the summary of changes as shown below:

Summary of Changes

Summary of Changes

If you want to compare multiple PDF files then you just need to add multiple target PDF files to the comparison as shown below:

comparer.Add("target2.docx");
comparer.Add("target3.docx");

Compare Password Protected PDF Files using C#

You can compare password-protected PDF documents by following the simple steps given below:

  1. Create an instance of the Comparer class
  2. Provide the source PDF file path to the constructor
  3. Provide a password for the source file using LoadOptions
  4. Add target PDF file to the comparison
  5. Provide a password for the target file using LoadOptions
  6. Call the Compare method along with the output file path

The following code sample shows how to compare password-protected PDF documents using C#.

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to compare two or more PDF documents and highlight differences using C#. You can learn even more about GroupDocs.Comparison .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also