Docotic.Pdf Library Help > Samples > General operations > Check that two PDF documents are equal

This sample shows how to check if two PDF documents are equal (i.e. have the same contents).

A PDF file consists of PDF objects. Some PDF objects contain unique data like content hash, creation time, etc. So you can not compare PDF files as byte streams because PDF documents with the same contents may have been saved with different compression, formatting and different unique data.

The PdfDocument.DocumentsAreEqual method compares two PDF documents as collections of PDF objects. It reports that documents are equal if all corresponding PDF objects are equal except some known fields (like CreationDate, or file IDs) that are really file properties and not contents properties.

C#
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class CompareDocuments
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            const string originalFile = @"Sample Data\Link.pdf";
            using (PdfDocument first = new PdfDocument(originalFile))
                first.Save("first.pdf");

            using (PdfDocument second = new PdfDocument(originalFile))
                second.Save("second.pdf");

            using (PdfDocument third = new PdfDocument(originalFile))
            {
                third.Pages[0].Canvas.DrawString("Hello");
                third.Save("third.pdf");
            }

            StringBuilder message = new StringBuilder();
            bool equals = PdfDocument.DocumentsAreEqual("first.pdf", "second.pdf", "");
            message.AppendLine("first.pdf equals to second.pdf?\r\n" + (equals ? "Yes" : "No"));

            message.AppendLine();

            equals = PdfDocument.DocumentsAreEqual("first.pdf", "third.pdf", "");
            message.AppendLine("first.pdf equals to third.pdf?\r\n" + (equals ? "Yes" : "No"));

            MessageBox.Show(message.ToString());
        }
    }
}
VB
Imports System.Diagnostics
Imports System.Text
Imports System.Windows.Forms

Imports Microsoft.VisualBasic

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class CompareDocuments
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Const originalFile As String = "Sample Data\Link.pdf"
            Using first As New PdfDocument(originalFile)
                first.Save("first.pdf")
            End Using

            Using second As New PdfDocument(originalFile)
                second.Save("second.pdf")
            End Using

            Using third As New PdfDocument(originalFile)
                third.Pages(0).Canvas.DrawString("Hello")
                third.Save("third.pdf")
            End Using

            Dim message As New StringBuilder()

            message.AppendLine("first.pdf equals to second.pdf?")
            If PdfDocument.DocumentsAreEqual("first.pdf", "second.pdf", "") Then
                message.AppendLine("Yes")
            Else
                message.AppendLine("No")
            End If

            message.AppendLine()

            message.AppendLine("first.pdf equals to third.pdf?")
            If PdfDocument.DocumentsAreEqual("first.pdf", "third.pdf", "") Then
                message.AppendLine("Yes")
            Else
                message.AppendLine("No")
            End If

            MessageBox.Show(message.ToString())
        End Sub
    End Class
End Namespace