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 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 installation command below.

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#.

using (Comparer comparer = new Comparer("C:\\Files\\source.pdf"))
{
comparer.Add("C:\\Files\\target.pdf");
comparer.Compare("C:\\Files\\result.pdf");
}
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:

C# PDF Comparison Changes

C# PDF Comparison Changes

Compare Multiple PDF Documents using C#

We have already discussed comparing two PDF documents in C# in the section above. Now, let us explore advanced scenarios when you need to compare multiple PDF files using C#.

For comparing multiple PDF files, you need to add multiple target PDF files to the comparison as shown with the following lines of code:

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

Compare Password Protected PDF Files using C#

You can compare password-protected PDF documents and highlight their differences 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 the 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#.

using (Comparer comparer = new Comparer("C:\\Files\\source.pdf", new LoadOptions() { Password = "1234" }))
{
comparer.Add("C:\\Files\\target.pdf", new LoadOptions() { Password = "5678" });
comparer.Compare("C:\\Files\\result.pdf");
}

Get a Free License

You can compare PDF documents in C# 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#. It empoyers you to compare multiple documents at once and then you can decide to accept or reject the changes accordingly. Furthermore, you can learn more about GroupDocs.Comparison .NET API from the documentation section. In case of any ambiguity, please feel free to contact us on the forum.

See Also