C#: Compare PDF Documents

PDF has become the standard format for sharing and preserving documents across different platforms, playing a ubiquitous role in both professional and personal settings. However, creating high-quality PDF documents requires multiple checks and revisions. In this context, knowing how to efficiently compare PDF files and pinpoint their differences becomes crucial, which enables document editors to quickly identify discrepancies between different versions of a document, resulting in significant time savings during the document creation and review process. This article aims to demonstrate how to compare PDF documents effortlessly using Spire.PDF for .NET in C# programs.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Compare Two PDF Documents in C#

With Spire.PDF for .NET, developers can create an instance of the PdfComparer class, passing two PdfDocument objects as parameters, and then utilize the PdfComparer.Compare(String fileName) method to compare the two documents. The resulting comparison is saved as a new PDF document, allowing for further analysis or review of the differences between the two PDFs.

The resulting PDF document displays the two original documents on the left and the right, with the deleted items in red and the added items in yellow.

The following are the detailed steps for comparing two PDF documents:

  • Create two objects of PdfDocument class and load two PDF documents using PdfDocument.LoadFromFile() method.
  • Create an instance of PdfComparer class and pass the two PdfDocument objects as parameters.
  • Compare the two documents and save the result as another PDF document using PdfComparer.Compare() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Comparison;

namespace ExtractTablesToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an object of PdfDocument class and load a PDF document
            PdfDocument pdf1 = new PdfDocument();
            pdf1.LoadFromFile("Sample1.pdf");

            //Create another object of PdfDocument class and load another PDF document
            PdfDocument pdf2 = new PdfDocument();
            pdf2.LoadFromFile("Sample2.pdf");

            //Create an object of PdfComparer class with the two document
            PdfComparer comparer = new PdfComparer(pdf1, pdf2);
            
            //Compare the two document and save the comparing result to another PDF document
            comparer.Compare("output/ComparingResult.pdf");
            pdf1.Close();
            pdf2.Close();
        }
    }
}

C#: Compare PDF Documents

Compare a Specific Page Range of Two PDF Documents

After creating an instance of PdfComparer class, developers can also use the PdfComparer.Options.SetPageRange() method to set the page range to be compared. This allows for comparing only the specified page range in two PDF documents. The detailed steps are as follows:

  • Create two objects of PdfDocument class and load two PDF documents using PdfDocument.LoadFromFile() method.
  • Create an instance of PdfComparer class and pass the two PdfDocument objects as parameters.
  • Set the page range to be compared using PdfComparer.Options.SetPageRange() method.
  • Compare the specified page range in the two PDF documents and save the result as another PDF document using PdfComparer.Compare() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Comparison;

namespace ExtractTablesToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an object of PdfDocument class and load a PDF document
            PdfDocument pdf1 = new PdfDocument();
            pdf1.LoadFromFile("Sample1.pdf");

            //Create another object of PdfDocument class and load another PDF document
            PdfDocument pdf2 = new PdfDocument();
            pdf2.LoadFromFile("Sample2.pdf");

            //Create an object of PdfComparer class with the two document
            PdfComparer comparer = new PdfComparer(pdf1, pdf2);

            //Set the page range to be compared
            comparer.Options.SetPageRanges(1, 1, 1, 1);
            
            //Compare the specified page range and save the comparing result to another PDF document
            comparer.Compare("output/PageRangeComparingResult.pdf");
            pdf1.Close();
            pdf2.Close();
        }
    }
}

C#: Compare PDF Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.