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 detects changes in words, paragraphs, and characters and creates a comparison document that lists a summary of differences. The .NET compare library checks content and text style for popular formats such as PDF, HTML, Outlook email, Word, Excel, PowerPoint, OneNote, Visio, text, and images. It works in any development environment that targets the .NET platform.

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

Install-Package GroupDocs.Comparison

Compare Two PDF Files using C#

You can compare two PDF documents by following these steps:

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

The code sample below 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, showing the changes as illustrated below:

C# PDF Comparison Changes

C# PDF Comparison Changes

Compare Multiple PDF Documents using C#

We have already covered comparing two PDF documents. Now let’s explore how to compare multiple PDF files in C#.

To compare several PDFs, add each target file to the comparison:

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 these steps:

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

The code sample below shows how to compare password‑protected PDF documents using C#.

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 enables you to compare multiple documents at once and decide whether to accept or reject the changes. For more details, see the GroupDocs.Comparison .NET API documentation at the documentation page. If you have questions, feel free to contact us on the forum.

See Also