Skip to main content

Command Palette

Search for a command to run...

Comparing Two PDF Documents Using C#

Published
3 min read

In daily development or office scenarios, PDF document comparison is a high-frequency requirement. Although there are visual comparison tools like Adobe Acrobat available on the market, implementing PDF comparison through programming can meet automated and customized business needs. This article will introduce how to compare two PDF documents using the free library Free Spire.PDF for .NET.


I. Core Component: The PdfComparer Class

PdfComparer is a high-level encapsulated class provided by Free Spire.PDF for .NET, designed specifically for PDF document comparison. It automatically analyzes content differences between two PDFs (such as text additions, deletions, and modifications) and outputs the comparison results as a new PDF document.

Presentation of Comparison Results

The comparison result document generated by PdfComparer adopts intuitive visual annotations:

  • Fully consistent PDFs: No highlight markers; only the content of the original documents is displayed.
  • Added content: Usually highlighted in yellow.
  • Deleted content: Usually marked with red highlights.

II. Install Free Spire.PDF

It is recommended to install the library via the NuGet Package Manager, following these steps:

  1. Open your project and right-click to select Manage NuGet Packages.
  2. Search for FreeSpire.PDF and install the latest stable version.

Alternatively, install it via the NuGet Command Line:

Install-Package FreeSpire.PDF

III. How to Compare Two PDF Documents Using C

Free Spire.PDF features an extremely simple comparison logic that eliminates the need for manual page processing or text extraction. The core process involves just 4 steps:

  1. Instantiate PdfDocument objects and load the two PDF files to be compared, respectively.
  2. Instantiate a PdfComparer object and pass in the two loaded PDF documents.
  3. Call the PdfComparer.Compare() method and specify the save path for the comparison result PDF.
  4. Release PDF document resources to prevent memory leaks.

Important Note: The free version has a certain page limit for processing PDFs, but it is sufficient for most basic comparison requirements.

Complete Code

The following few lines of simple code enable the comparison of text content between two PDF documents:

using Spire.Pdf;
using Spire.Pdf.Comparison;

namespace ComparePDF
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load the two PDF documents to be compared
            PdfDocument pdf1 = new PdfDocument();
            pdf1.LoadFromFile("Sample1.pdf");

            PdfDocument pdf2 = new PdfDocument();
            pdf2.LoadFromFile("Sample2.pdf");

            // Instantiate PdfComparer and pass in the two documents
            PdfComparer comparer = new PdfComparer(pdf1, pdf2);

            // Perform comparison and save the result PDF
            comparer.Compare("ComparingResult.pdf");
            pdf1.Close();
            pdf2.Close();
        }
    }
}

The C# PDF comparison solution based on the PdfComparer class is extremely simple and efficient. It eliminates the need to manually handle underlying comparison logic and can directly generate a PDF result document with difference markers, which is more readable than plain text difference logs.

More from this blog